Search code examples
androidrestretrofitretrofit2

Retrofit Square Brackets Query Parameters


I have the following GET URL that requires multiple parameters:

https://www.games.com/game?filter[condition][path]=season&filter[condition][value]=Season1&page[limit]=1&page[offset]=0

However, I can't find a way to represent this parametrized call using Retrofit because it's using Square Brackets.

I've tried something like this, but it doesn't work, since Retrofit cannot recognize those parameters.

@Headers("Content-Type: application/json")
@GET("game")
Call<FunctionsResponse> getAllGames(
        @Query("filter[condition][path]") String conditionFilter,
        @Query("filter[condition][value]") String conditionValue,
        @Query("page[limit]") String pageLimit,
        @Query("page[offset]") String pageOffset
);

Any hint?


Solution

  • Try this and let me know if you still got error

    @Headers("Content-Type: application/json")
    @GET("game")
    Call<FunctionsResponse> getAllGames(
            @Query("filter%5Bcondition%5D%5Bpath%5D") String conditionFilter,
            @Query("filter%5Bcondition%5D%5Bvalue%5D") String conditionValue,
            @Query("page%5Blimit%5D") String pageLimit,
            @Query("page%5Boffset%5D") String pageOffset
    );
    

    In this code, the square brackets [ and ] are encoded as %5B and %5D, respectively, using URL encoding. Retrofit will properly encode the query parameters with square brackets and include them in the request URL.