Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send multipart with files using retrofit:2.0.0-beta1

I have problem with sending multipartRequest to 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:

request body is empty, when it come to server

like image 688
kadik Avatar asked Nov 10 '22 01:11

kadik


1 Answers

Update

Uploading files with Retorfit 2 and Multipart

Original, off-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. This 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);
}
like image 169
Miha_x64 Avatar answered Nov 14 '22 22:11

Miha_x64