Search code examples
androidretrofit2

how to send List<String> with retrofit?


I'm sending a multipart request to server and this is my interface:

@Multipart
@POST("v1/group/new")
Call<MyResponse> newGroup(
        @Header("token") String token,
        @Part MultipartBody.Part photo,
        @Part("title") RequestBody subject,
        @Part("members") List<RequestBody> members);

and for sending my members in my fragment, I change my List<String> to List<RequestBody> as below:

List<RequestBody> members = new ArrayList<>();
for(int i = 0;i < membersId.size(); i++){
    members.add(RequestBody.create(MediaType.parse("text/plain"),membersId.get(i)));
}

and it's working with multiple members! but when there is a one string in my list, retrofit doesn't sends my members as a list!!! for example:

I want to send array of strings like this :

["item1","item2","item3"]

my code works for this, but when there is only one item, retrofit sends this :

"item1"

instead of ["item1"]

what is the proper way of sending array of string in multipart with retrofit?

what am I doing wrong?


Solution

  • Use something like this.

    @Multipart
    @POST("v1/group/new")
    Call<MyResponse> newGroup(
            @Header("token") String token,
            @Part MultipartBody.Part photo,
            @Part("title") RequestBody subject,
            @Part("members[]") List<RequestBody> members);
    

    Remember you must add [] to your members param :).