Send multipart with files using modification: 2.0.0-beta1

I have a problem sending multipartRequest to the server.

RequestBody file = RequestBody.create(MediaType.parse("application/json"), myFile); return apiService.updateProfile2(token, file); @Multipart @POST("/profile/update") Call<RegistrationResponse> updateProfile2(@Header(value = "X-AUTH-TOKEN") String toke, @Part(value = "json") RequestBody json); 

problem:

The request body is empty when it arrives at the server

+5
source share
2 answers

Update

Download files from Retorfit 2 and Multipart

Original non-topic answer

http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically Here is a good article with code that sends multipart. It worked for me.

 MultipartUtility multipart = new MultipartUtility(requestURL, charset); multipart.addHeaderField("User-Agent", "CodeJava"); multipart.addHeaderField("Test-Header", "Header-Value"); multipart.addFormField("description", "Cool Pictures"); multipart.addFormField("keywords", "Java,upload,Spring"); multipart.addFilePart("fileUpload", uploadFile1); multipart.addFilePart("fileUpload", uploadFile2); List<String> response = multipart.finish(); System.out.println("SERVER REPLIED:"); for (String line : response) { System.out.println(line); } 
+1
source

First, you are trying to send mediaType "application / json" as @Multpart if you want to send a multi-part file, so you need to change something in your code.

Something like that:

 @Multipart @POST("/profile/update") Call<RegistrationResponse> updateProfile2( @Header(value = "X-AUTH-TOKEN") String toke, @Part("myfile\"; filename=\"image.png\" ") RequestBody file); 

And change the calling method to this:

 RequestBody file = RequestBody.create(MediaType.parse("multipart/form-data"), myFile); return apiService.updateProfile2(token, file); 
0
source

Source: https://habr.com/ru/post/1235883/


All Articles