Why can't Retrofit correctly encode a query string with square brackets?

I use Retrofit for my network layer in my Android app, but I have a problem with URL encoding.

I need to call the REST API:

https://my_hostname.com/some_path?q=some_query&param[0]=value1&param[1]=value2&other_param=abcd 

since you can see that the query string consists of several parameters, so I decided to use the @QueryMap annotation in the Retrofit interface with Map<String, String> , where q, param[1], param[0], other_param are map string keys

What am I expecting? I expect the square brackets in the URL are encoded with %5B for '[' and %5D for '[' , but this does not happen.

Why is this happening? Square brackets must be encoded with percent encoding. Is this a mistake, or am I doing something wrong? I also tried the @EncodedQueryMap annotation without any differences.

+5
source share
2 answers

Request names are never URL encoded.

The documentation for @QueryMap states:

Values ​​are URL encoded.

And for @EncodedQueryMap :

Values ​​are not URL encoded.

However, I just sent a transfer request to slightly change this behavior. I am adding support for encoding keys using @Query(value = "..", encodeName = true) or @QueryMap(encodeNames = true) .

+13
source

Try simple @ GET and @Query options

+1
source

Source: https://habr.com/ru/post/1203748/


All Articles