kadik
kadik

Reputation: 149

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

Upvotes: 5

Views: 328

Answers (2)

Miha_x64
Miha_x64

Reputation: 6363

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);
}

Upvotes: 1

Anderson K
Anderson K

Reputation: 5505

First all, do you are trying send the mediaType "application/json" as @Multpart, if you want send the multpartfile so do you needs change somethings in your code.

Something like this:

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

And change call method to this:

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

Upvotes: 0

Related Questions