Is it possible to pass one element as multipart using modification?

public void addDeal(@Body Deal deal, @Part("image")TypedFile image,Callback<Response> callback);


I only want to send the image as multi-page and the rest as is. Is there any possible way? Even I tried to add TypedFile inside my Deal model but couldn't comment @part


source to share

3 answers

Yes it is possible with Partmap annotation using hashmap. For example -

    Call<Response> addDeal(@PartMap 
    HashMap<String, RequestBody> hashMap);


In hashMap you can add any url parameters as key and set your values ​​using RequestBody class type. how to convert String and Image to RequestBody.

    public static RequestBody ImageToRequestBody(File file) { //for image file to request body
           return RequestBody.create(MediaType.parse("image/*"),file);

    public static RequestBody StringToRequestBody(String string){ // for string to request body
           return RequestBody.create(MediaType.parse("text/plain"),string);


add parameters to hashmap -

    hashMap.put("photo",ImageToRequestBody(imageFile)); //your imageFile to Request body.
    //calling addDeal method


Hope it helps.



It seems that you can send your @Body as TypedString.
For example, convert the transaction "@Body Deal" to a JSON String and send it as a TypedString.

Details: How to submit multipart / form data using Retrofit?

Observable<Response> uploadFile(@Part("author") TypedString authorString,
                                @Part("photo") TypedFile photoFile);




This worked for me: POST Multipart Form Data using Retrofit 2.0 including image

public interface ApiInterface {

    Call<User> editUser (@Header("Authorization") String authorization, @Part("file\"; filename=\"pp.png\" ") RequestBody file , @Part("FirstName") RequestBody fname, @Part("Id") RequestBody id);

    File file = new File(imageUri.getPath());
    RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
    RequestBody name = RequestBody.create(MediaType.parse("text/plain"), firstNameField.getText().toString());
    RequestBody id = RequestBody.create(MediaType.parse("text/plain"), AZUtils.getUserId(this));
    Call<User> call = client.editUser(AZUtils.getToken(this), fbody, name, id);

    call.enqueue(new Callback<User>() {
        public void onResponse(retrofit.Response<User> response, Retrofit retrofit) {

        public void onFailure(Throwable t) {




All Articles