Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kayvannj opened this issue Apr 16, 2015 · 5 comments

Comments

@kayvannj
Copy link

I found this issue and my problem is very similar to it
#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:

--84ff8f5f-69ce-49e4-bc16-e43e221a6e2b
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:

--Boundary+0xAbCdEfGbOuNdArY
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.

@JakeWharton
Copy link
Collaborator

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

@kayvannj
Copy link
Author

Ok, Thanks Jake.

@JakeWharton
Copy link
Collaborator

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

@Bajranghudda1
Copy link

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<String, RequestBody> 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"]}}

@KweeWaiYan
Copy link

@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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants