0

我想測試一個API後調用,它允許文件上傳,但我不能得到它的工作,我總是收到301Django的上傳文件測試返回301

with open('video.mp4') as f: 
    data = {'file': f} 
    response = self.client.post('/api/upload_file', data, format='multipart') 

返回的響應是301

HttpResponsePermanentRedirect status_code=301, "text/html; charset=utf-8", url="/api/v1/assets/upload_file/" 

我確保self.client進行身份驗證和測試的其餘部分正確

self.client = APIClient() 
self.client.force_authenticate(user=self.user) 

回答

1

你跑錯在你的測試中跟蹤斜線,所以Django會自動重定向,因爲你有APPEND_SLASH = True

要解決這個問題,你的代碼更改爲:

self.client.post('/api/upload_file/', data, format='multipart')