public interface PostMessage {
@Multipart
@POST("https://www.example.com/message")
void sendMessage(@QueryMap Map
@Part("image") TypedFile imagefile, Callback
}
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 ?
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
@Part("image") TypedFile imagefile, Callback
}
@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
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.
@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
@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?
@abirdas33 check out this link, it should help you.
https://stackoverflow.com/questions/39820762/using-fieldmap-and-part-in-single-request-in-retrofit2-gets-java-lang-illegala
@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);

How to call retrofit request for dynamic type key?
Most helpful comment
@Abhinav-git : try the below code :
public interface PostMessage { partMap, response);
@Multipart
@POST("https://www.example.com/message")
void sendMessage(@PartMap Map
@Part("image") TypedFile imagefile, Callback
}