Retrofit: How to send array of Strings with multipart request to result in a[]="S1" a[]="S2" ...

Created on 16 Apr 2015  路  5Comments  路  Source: square/retrofit

I found this issue and my problem is very similar to it
https://github.com/square/retrofit/issues/472

the only difference is that I want this to happen on multipart-post requests.

Currently it will post the array/list as one part like this:

Content-Disposition: form-data; name="user_ids[]"
Content-Type: application/json; charset=UTF-8
Content-Length: 21
Content-Transfer-Encoding: binary

["5531687","5371507"]

What I need is this:

Content-Disposition: form-data; name="user_ids[]"

5531687
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="user_ids[]"

5371507

here is how I create the request:

@Multipart
@POST("/api/v1/inbox")
void conversationPostNewMessage(@Part("user_ids[]") String[] ids, @Part("attachments[]") TypedFile attachment, Callback<Object> callback);

I'm using v1.9.0.

Thank you in advance.

Most helpful comment

Yes can send an array or list also like this...

RequestBody rb;
    LinkedHashMap<String, RequestBody> mp= new LinkedHashMap<>();
    for(int i=0;i<itemBeam.getItems_info().size();i++)
    {
               rb=RequestBody.create(MediaType.parse("text/plain"), itemBeam.getItems_info().get(i).getItem_bar_code());
        mp.put("item_bar_code["+i+"]", rb);

        rb=RequestBody.create(MediaType.parse("text/plain"),itemBeam.getItems_info().get(i).getItem_type());
        mp.put("item_type["+i+"]",rb);

        rb=RequestBody.create(MediaType.parse("text/plain"),itemBeam.getItems_info().get(i).getCna_number());
        mp.put("cna_number["+i+"]",rb);

        rb=RequestBody.create(MediaType.parse("text/plain"),itemBeam.getItems_info().get(i).getTime());
        mp.put("times["+i+"]",rb);
    }

Now pass your map in the callback function ....

getVerty(mp);

And change your interface like this.....
@Multipart
@POST("vertical")
Call getVerty(
@PartMap() Map phot,
);

That's it.....
now your request will be like....
{"status":"1","message":{"item_bar_code":["MC140517193S","9804456119139","CN04G4817161653H01AV","SP40D71289"],"item_type":["O","O","O","SP"],"cna_number":["","","",""],"times":["2015-12-17 10:04:33","2015-12-17 10:04:38","2015-12-17 10:04:46","2015-12-17 10:04:53"]}}

All 5 comments

We don't currently support lists or arrays for @Part. I suppose for parity with the other annotations we should.

Ok, Thanks Jake.

This is a dupe of #685. We'll get this in for 2.0.

Yes can send an array or list also like this...

RequestBody rb;
    LinkedHashMap<String, RequestBody> mp= new LinkedHashMap<>();
    for(int i=0;i<itemBeam.getItems_info().size();i++)
    {
               rb=RequestBody.create(MediaType.parse("text/plain"), itemBeam.getItems_info().get(i).getItem_bar_code());
        mp.put("item_bar_code["+i+"]", rb);

        rb=RequestBody.create(MediaType.parse("text/plain"),itemBeam.getItems_info().get(i).getItem_type());
        mp.put("item_type["+i+"]",rb);

        rb=RequestBody.create(MediaType.parse("text/plain"),itemBeam.getItems_info().get(i).getCna_number());
        mp.put("cna_number["+i+"]",rb);

        rb=RequestBody.create(MediaType.parse("text/plain"),itemBeam.getItems_info().get(i).getTime());
        mp.put("times["+i+"]",rb);
    }

Now pass your map in the callback function ....

getVerty(mp);

And change your interface like this.....
@Multipart
@POST("vertical")
Call getVerty(
@PartMap() Map phot,
);

That's it.....
now your request will be like....
{"status":"1","message":{"item_bar_code":["MC140517193S","9804456119139","CN04G4817161653H01AV","SP40D71289"],"item_type":["O","O","O","SP"],"cna_number":["","","",""],"times":["2015-12-17 10:04:33","2015-12-17 10:04:38","2015-12-17 10:04:46","2015-12-17 10:04:53"]}}

@NonNull
private Map<String,RequestBody> createPartFromArray(String[] skills)
{
    Map<String, RequestBody> skill = new HashMap<String, RequestBody>();
    RequestBody requestFile ;
    for(int i=0 ;i<skills.length;i++) {
        requestFile = RequestBody.create(MultipartBody.FORM,skills[i]);
        skill.put("skill["+i+"]", requestFile);
    }
   return skill;

}
Was this page helpful?
0 / 5 - 0 ratings