POST Requestbody and Multipart in retrofit2

Asked

Viewed 52 times

0

How to send an image and jsonObject to the webservice in a single request in retrofit2?

Interface

@Multipart
@POST("url")
Call<Mensagens> setChatMessage(@HeaderMap Map<String,String> headers, @Body RequestBody params, @Part MultipartBody.Part img);

Class

RequestBody body = RequestBody.create(MediaType.parse("application/json"), params.toString());
                RequestBody reqFileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part mPart1 = MultipartBody.Part.createFormData("file","file",reqFileBody);
                service = new RetrofitConfig().getServices().setChatMessage(headers, body, mPart1);

Error

@Body parameters cannot be used with form or multi-part encoding. (parameter #2)
  • Better send the image as an attribute of the object itself.

  • In case you can take a look at the stack itself has a very good link that can help. https://stackoverflow.com/questions/39953457/how-to-upload-image-file-in-retrofit-2/56412584

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.