I need to send a multipart request using the retrofit 2.0 with image and some key-value parameters: "key1" - "parameter1"
, "key2" - "parameter2"
etc. But there are parameters with the same key: "somepar[]" - "text1"
, "somepar[]" - "text2"
... And i cant use @PartMap
in this structure:
@Multipart
@POST(myUrlPart)
Call<ClassEntity> myRequest(@Header("Authorization") String authHeader,
@Part("image\"; filename=\"image.png\"") RequestBody image,
@PartMap Map<String, RequestBody> params);
because Map<> cant store multiple values with the same key. And i cant use
@Part("somepar[]") List<String> mylist
or
@Part("somepar[]") String[] myArray
because it will send key-value "somepar[]" - "{"1","2","3"}"
, not the "somepar[]" = "1"
, "somepar[]" = "2"
, "somepar[]" = "3"
.
Please help, how to make such a request.
You can send a POST / PUT request by either submitting a body depending on the API Content Type Form Data, Form URL Encoded, or using JSON. To submit a JSON object you can use the @SerializedName to specify how to send each field data in the request.
Annotation Type PartMapDenotes name and value parts of a multi-part request. Values of the map on which this annotation exists will be processed in one of two ways: If the type is RequestBody the value will be used directly with its content type.
Okay, this problem was solved in new verions of retrofit library (2.1.0 version at the moment writing this post). This code will be work correctly and send data correctly:
@Part("somepar[]") List<String> mylist
Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With