0

Retrofit 2.4.0

retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

I need to POST a List of objects and a list of pictures in the same request, like this :

@Multipart
@POST("api/debug/{id}/")
Call<ItemResponse> uploadItems(
        @Path("id") int id,
        @Part("items[]") List<Item> items,
        @Part List<MultipartBody.Part> pictures);

Server side the list of items are not serialized correctly :

{
  "items": [
    "{"description":"desc1","id":1,"title":"title1"}",
    "{"description":"desc2","id":2,"title":"title2"}",
    "{"description":"desc3","id":3,"title":"title3"}"
  ],
  "picture1": {
    "name": "picture1",
    "size": "315.777KB"
  },
  "picture2": {
    "name": "picture2",
    "size": "207.821KB"
  }
}

The problem seems to be with using @Part.

What's the trick?

R.Tissier
  • 108
  • 7

1 Answers1

0

Well, can't you just replace @Part("items[]") with @Part? Then you will get the same structure in JSON response as the picture's list structure.

Marcin Rzepecki
  • 578
  • 4
  • 10
  • If I do this an error is thrown : @Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #2) – R.Tissier Oct 20 '20 at 08:49
  • Okay, I see the problem. I think that this solution will help you: https://stackoverflow.com/a/58951111/14056755. TL;DR You have to user @PartMap annotation for the list of objects existing along with the Multipart objects. – Marcin Rzepecki Oct 20 '20 at 09:06