0

The server API takes a JSON array in the request body. Like this:

[
  {
    "key1" : "value1",
    "key2" : "value2",
    "key3" : "value3"
  }
]

I know how to put a JSON object as the body, but how can I put a JSON array in a retrofit request?

I already have a POJO:

public class Sample {
    @SerializedName("key1")
    @Expose
    private String key1;
    @SerializedName("key2")
    @Expose
    private String key2;
    @SerializedName("key3")
    @Expose
    private String key3;
}

Here's my API class:

  @PATCH("//some url")
    Observable<ResponseBody> patchRequest(
            // ... other params
            @Body Sample sampleBody;
    );

When I make a request, the body looks like this:

  {
    "key1" : "value1",
    "key2" : "value2",
    "key3" : "value3"
  }

Except I need it to be inside an array as show in the first JSON.

kyrax
  • 1,192
  • 3
  • 13
  • 29

2 Answers2

0

Use list of that objects

@POST("url")
Call<Object> doStuff(@Body List<Map<String, String>> body)
Mihail
  • 241
  • 1
  • 6
0

Return a list of Samples. It will then wrapped into a JSON array.

@PATCH("//some url")
Observable<ResponseBody> patchRequest(
        // ... other params
        @Body List<Sample> sampleBody;
);
meistermeier
  • 7,942
  • 2
  • 36
  • 45
Anjul khanal
  • 147
  • 1
  • 6