2016-11-29 37 views
0

我在Python上編寫了API服務,並在Postman中爲它提供了文檔。 Postman中的所有請求都很好,但是從文件上傳請求生成的Java代碼(返回OkHttp庫)返回「內部服務器錯誤」,我檢查了服務器日誌,發現該文件未送達,它在進入views.py之前崩潰。從郵差從Postman生成的代碼在Android項目中無法正常工作

請求截圖(令牌和地址被隱藏,但他們正確)

截圖從郵差:

1)Headers screenshot

2)Body screenshot

的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());} 
+0

試試這個http://stackoverflow.com/a/40524159/4238189 – young

+0

@mablevj,但我添加文件請求,而不是json對象 – lucas63

回答

0

此代碼正常工作,我只是忘記添加讀/寫權限。