0
我在Python上編寫了API服務,並在Postman中爲它提供了文檔。 Postman中的所有請求都很好,但是從文件上傳請求生成的Java代碼(返回OkHttp庫)返回「內部服務器錯誤」,我檢查了服務器日誌,發現該文件未送達,它在進入views.py之前崩潰。從郵差從Postman生成的代碼在Android項目中無法正常工作
請求截圖(令牌和地址被隱藏,但他們正確)
截圖從郵差:
的Java代碼
private final OkHttpClient client = new OkHttpClient();
public void run(String fileName) throws IOException {
String url = "http://*****:8025/v1/add_new_record/";
File file = new File(fileName);
RequestBody formBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file_url", file.getName(),
RequestBody.create(MediaType.parse("text/plain"), file))
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.addHeader("Authorization", "Token *****")
.build();
Response response = this.client.newCall(request).execute();
Log.i("data",response.message());}
試試這個http://stackoverflow.com/a/40524159/4238189 – young
@mablevj,但我添加文件請求,而不是json對象 – lucas63