I'm Using the below code in Android, retrofit, to upload an image:
#Multipart
#POST("uploadimage")
Call<ImageUploadResponse> uploadImage(#PartMap Map<String, RequestBody> map);
But what if I need to send extra data, such as Image Description along with the request?
I Was not able to use #Field, that is i tried like this:
#Multipart
#FormUrlEncoded
#POST("uploadimage")
Call<ImageUploadResponse> uploadImage(#PartMapMap<String,RequestBody> map,
#Field("description")String desc);
I got an error stating that only one annotation is allowed. That is either #Multipart or #FormUrlEncoded.
You can use #Part instead of #Field
#Multipart
#POST("uploadimage")
Call<ImageUploadResponse> uploadImage(#PartMap Map<String, RequestBody> map, #Part("description") String description);
Related
I want to send image , name and city for the server and all of these can be optional but i don't know how the interface will look like i found a solution with overloading the methods but it makes the code complicated
The http method is PUT also .
Use the annotation for Multipart. Create a Map of city and name to be used as PartMap. Pass the image as MultipartBody. Something like this:
#Multipart
#POST("")
Call<ReturnType> yourCall(#Url String url, #PartMap() Map<String, RequestBody> params, #Part MultipartBody.Part multipart, ...);
I can't upload photo to server. java.lang.IllegalArgumentException: #Field parameters can only be used with form encoding. (parameter #1). Please help me, how to solve or any other suggestions. postman
#FormUrlEncoded
#Multipart
#POST("qq/api/xxxx")
Call<Custom> postCustom(#Field("Id") String Id,
#Part MultipartBody.Part file,
#Field("Status") String Status);
Change your request interface like this
#Multipart
#POST("qq/api/xxxx")
Call<Custom> postCustom(
#Part("Id") String Id,
#Part MultipartBody.Part file,
#Part("Status") String Status);
You cannot use both #FormUrlEncoded and #Multipart on a single method since an HTTP request can only have one Content-Type. #FormUrlEncoded and #Multipart are both content types.
From Jake Wharton
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.
References
I have added also these issue reference which is really helpful for you to understanding what you are about to change in your http request method
https://github.com/square/retrofit/issues/662
https://github.com/square/retrofit/issues/1210
REST-API PUT with both #Path and #Body as input parameters
I am trying to create an API PUT method which should use both #Path and #Body as input parameters but cannot find the exact example(sample code) to achieve that.
This is a sample for your requirement.
#PUT("/api/{username}")
void putAPICall(#Path("username") String username, #Body RequestBody params);
Here is my sample code.
#Multipart
#POST("/upload-end-users-document")
Call<ListPinDevicesResponse<UploadEndUsersDocumentResponse>> getUploadEndUsersDocument(#PartMap Map<String, String> userParams,#PartMap Map<String, RequestBody> params,#Header("access-token") String accessToken);
is there a no way to use #Field and #part together in Retrofit??
If yes than tell a reason, If no tell me a proper way
I will appreciate your answer and thank you in advance.
Screenshots of Postman tool is attached in the link below.
https://drive.google.com/file/d/1J-MkO49gfXpYKwxyBqtzHqotnCgHJ9yp/view?usp=sharing
https://drive.google.com/file/d/1d1WZPHVSxFsvyY4zvXVmd9OBYcsSn3yG/view?usp=sharing
This might seem similar to earlier questions but none actually answers my question.
I need to Post multiple fields and multiple images in one request using retrofit2 and i'm getting this error
java.lang.IllegalArgumentException: Only one encoding annotation is allowed.for method xxx
i'm using
#Multipart
#FormUrlEncoded
since #Field requires #FormUrlEncoded and #Part requires #Multipart.
the more logical thing to do is to remove the #FormUrlEncoded annotation, but how do i go from there.
Now the question is how do i go about the task to achieve sending my post in a single request.
here's the interface
#Multipart
#FormUrlEncoded
#POST("upload")
Call<ResponseBody> uploadPost(#FieldMap Map<String, String> map,
#Part MultipartBody.Part image1,
#Part MultipartBody.Part image2,
#Part MultipartBody.Part image3);
#Multipart
#POST("upload")
Call<ResponseBody> uploadPost(
#PartMap() Map<String, RequestBody> descriptions,
#Part List<MultipartBody.Part> images);
use this interface.