Android Guy
Android Guy

Reputation: 583

Retrofit: Multipart with json

I want to send json with Multipart form data.

I am familiar with header + multipart form data but the issue with me headers not allowed to send chinese/hebrew characters. So I need to use json for this.

Can anyone help me out in this?

My JSON ::

{
        "agent": "ee",
        "phone": "123",
        "manager": "234"
}

form-data having Multipart with "image" parameter.

Upvotes: 2

Views: 5440

Answers (1)

AbhayBohra
AbhayBohra

Reputation: 2117

Try this

    RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), selectedImage /* file name*/);
    MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", selectedImage.getName(), fileBody);

agent = RequestBody.create(MediaType.parse("text/plain"), "<agent-value>");
phone = RequestBody.create(MediaType.parse("text/plain"), "<phone-value>");
manager = RequestBody.create(MediaType.parse("text/plain"), "<manager-value>");

and in your interface class

  @Multipart
@POST(UPDATE_PROFILE_IMAGE)
Call<JsonObject> updateImage(@Part MultipartBody.Part image,
                             @Part("agent") RequestBody agent,
                             @Part("phone") RequestBody phone,
                             @Part("manager") RequestBody manager);

It works on all device

Upvotes: 3

Related Questions