Retrofit 2 send an empty array

Viewed 1607

I'm sending an array of integers to the backend via this Retrofit interface:

@PATCH("save/ids")
@FormUrlEncoded
Call<Output> saveIds(@Field("ids[]") List<Integer> ids);

Now this works when I have an ArrayList with some items. But to reset all the ids the servers wants an empty array called ids. When I send an empty array, Retrofit doesn't send the array - it just drops the parameter.

I create my ArrayList as follows:

List<Integer> ids = new ArrayList<>();
for (FooObjects object : listOfIds) {
    if (object.isEnabled()) {
        ids.add(object.getId());
    }
}

How can I send an empty array anyway?

2 Answers

I had this situation I managed to deal with it by telling the backend developer which im working with to accept a list contains an empty string and he will deal with it as empty list and it worked fine with me just made simple condition in the parameter of the service

if(list.isEmpty()) listOf(“”) else list
Related