Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant send retrofit 2.0 multipart request with parameters with the same keys

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.

like image 485
Alex D. Avatar asked Dec 09 '15 09:12

Alex D.


People also ask

How do you send files using multipart form data in retrofit?

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.

What is PartMap?

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.


1 Answers

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.

like image 187
Alex D. Avatar answered Sep 22 '22 02:09

Alex D.