Retrofit: Retrofit Form Encoded and Multipart in single request

Created on 20 Nov 2014  路  13Comments  路  Source: square/retrofit

public interface PostMessage {
@Multipart
@POST("https://www.example.com/message")
void sendMessage(@QueryMap Map queryMap,
@Part("image") TypedFile imagefile, Callback response);
}

I tried encryption of queryMap with @Feild and @Body. But it's giving IllegalArgumentException with message "Only one encoding annotation is allowed".

My Question is :
In a same request can data encryption done with multipart ?

Most helpful comment

@Abhinav-git : try the below code :

public interface PostMessage {
@Multipart
@POST("https://www.example.com/message")
void sendMessage(@PartMap Map partMap,
@Part("image") TypedFile imagefile, Callback response);
}

All 13 comments

You can have to use FormUrlEncodedTypedOutput as a @Part argument for the form encoded part and build it up yourself. The annotation on the method is for the outermost encoding which in this case is multipart.

Thanx :)

Hey .. can you please provide any sample code. I am also facing similar issue and unable to resolve

I want to send JSONObject as request (@Body) and attach one image file

@Abhinav-git : try the below code :

public interface PostMessage {
@Multipart
@POST("https://www.example.com/message")
void sendMessage(@PartMap Map partMap,
@Part("image") TypedFile imagefile, Callback response);
}

@JakeWharton I have the following request:

@Multipart
@FormUrlEncoded
@POST(Endpoints.KID_PROFILE_BASE + "/{uuid}" + Endpoints.POST_KID_PROFILE_END)
void postKidProfilePhoto(@Path(value = "uuid", encode = false) String startuuid, @Part("post[body]") TypedString requestJson, Callback callback);

Which throws the error:
Only one encoding annotation is allowed.

I am not sure how to refactor this to make it work with both FormUrlEncoded and Multipart? Could you explain what you had said a little more?

You cannot use both @FormUrlEncoded and @Multipart on a single method. An HTTP request can only have one Content-Type and both of those are content types.

@JakeWharton @Tukajo @Shanki070
Not clearly understand how to send form with multi-part data...

@Multipart
    @POST("api/upload/document.json")
    Call<User> uploadDocument(
            @QueryMap Map<String, Object> queryParams,
            @PartMap Map<String, RequestBody> params);

The server didn't see @QueryMap parameters it is required form @FormUrlEncoded annotation and @FieldMap as arguments.

queryParams look like this

Map<String, Object> params = new HashMap<>();
params.put("profile[firstName]", Utils.normalizeName(cardDuplex.getNameFirst()));
params.put("profile[lastName]", Utils.normalizeName(cardDuplex.getNameLast()));

params like this

 Map<String, RequestBody> params = new HashMap<>();
 params.put("profile[doc]\"; filename=\"doc.png\"",
                    RequestBody.create(MediaType.parse("image/png"), Utils.getBytes(doc)));

As you mentioned

You cannot use both @FormUrlEncoded and @Multipart on a single method

OK I use multipart but server didn't see @QueryMap it expects @FieldMap which is used only when @FormUrlEncoded annotation present. How can I send form data with multipart annotation?? Or it is the server issue and all should work?

@JakeWharton we cannot use both @FormUrlEncoded with @Body and @FieldMap together!!

I'm getting Error : Retrofit - @Body parameters cannot be used with form or multi-part encoding

@FormUrlEncoded
        @POST("/server/getHotels")
        Call<Test> createJsontest(@Body User usrjson, @FieldMap HashMap<String,String>  hmmap);

@JakeWharton @Shanki070 I think i'm faced with the same issue using retrofit2 with @FieldMap and @Part where @FormUrlEncoded is needed for @FieldMap and @Multipart is needed for @Part. i really dnt know how to go about this now, as i'm not really grounded in file uploads.
asked the same question on SO and no reply yet.

https://stackoverflow.com/questions/39820762

@JakeWharton @Shanki070
i used async http library and i used
requestParams.put("picture", Utils.SaveImage(context,imagePath),"image/jpeg");
requestParams.setUseJsonStreamer(false);
to post picture along with other data
how do i do setUserJsonStreamer(false) in retrofit.

@Multipart
@Part("new_user_registration.php")
Call newUserRegistrationCall{
@Part("fullname") String fullname,
@Part("phone") String phone,
@Part("username") String username,
@Part("password") String password
}

i am getting error in these code, it is written annotation is not allowed here. what should i do?

@JakeWharton we cannot use both @FormUrlEncoded with @Body and @FieldMap together!!

I'm getting Error : Retrofit - @Body parameters cannot be used with form or multi-part encoding

@FormUrlEncoded
        @POST("/server/getHotels")
        Call<Test> createJsontest(@Body User usrjson, @FieldMap HashMap<String,String>  hmmap);

Screen Shot 2020-07-07 at 12 13 08 PM
How to call retrofit request for dynamic type key?

Was this page helpful?
0 / 5 - 0 ratings