retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

@Path may not be encoding all reserved characters, for instance brackets

Open ttjst opened this issue 6 years ago • 7 comments

        RequestBuilder builder = new RequestBuilder(
                "GET",
                HttpUrl.get("https://github.com"),
                "square/{name1}/{name2}",
                null,
                null,
                false,
                false,
                false
        );
        builder.addPathParam("name1", "retro{}fit", false);
        builder.addPathParam("name2", "iss[]ues", false);
        assertEquals("https://github.com/square/retro%7B%7Dfit/iss%5B%5Des", builder.build().url().toString());

From rfc3986#section-3.3 I would assume that also [ and ] characters that are part of a path segment would need to be percent-encoded?

ttjst avatar Oct 26 '18 13:10 ttjst

I'll look into this next week unless @swankjesse wants to beat me to the punch (but that seems unlikely because we're both busy for the same reason). For unfortunate reasons we have to duplicate the encoding choices of OkHttp when it comes to paths. So whatever it does, we should be doing, and it is where the conversations about why should be had. That being said, maybe we want to write some kind of test which samples the entire ASCII space and some other interesting ranges to ensure we always match beahvior.

JakeWharton avatar Apr 23 '19 03:04 JakeWharton

We changed some APIs for query parameters in OkHttp 3.10. For path parameters it depends on which method you call. Retrofit might be telling OkHttp to retain the encoding here. Unfortunately fixing this risks breaking other users.

https://publicobject.com/2017/08/01/url-encoding-is-material/

swankjesse avatar Apr 23 '19 04:04 swankjesse

Hi guys,

Do you have any update on that issue ? Did you have the time to discuss about a solution or a fix ?

Thank you

mink-lparrouy avatar Apr 29 '19 11:04 mink-lparrouy

Hello, I work with mink-lparrouy, any news on this ?

AndoniLarz avatar May 02 '19 07:05 AndoniLarz

Updates will be posted as comments on the issue

On Thu, May 2, 2019 at 3:50 AM Andoni [email protected] wrote:

Hello, I work with mink-lparrouy, any news on this ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/square/retrofit/issues/2940#issuecomment-488581289, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAQIELMKL2NDMXQNBHNQGTPTKMNXANCNFSM4F7Q2PCA .

JakeWharton avatar May 02 '19 12:05 JakeWharton

+1 ran into this problem also

chris-kennedy avatar Aug 07 '20 20:08 chris-kennedy

Ran into it today. I wanted to enforce a pre-encoded string, so, I replaced the String path parameter with a String wrapper. The wrapper's toString() method is overriden to return the encoded value of the wrapped string.

Before:

get(@Path("key") value: String)

After:

get(@Path("key") value: UriString)

where UriString is:

data class UriString(private val value: String) { /** * @return uri encoded [value] */ override fun toString(): String { return Uri.encode(value) } }

I'd have preferred doing it with an interceptor or something attached to the client itself, but the request url is already fully-formed by the time it is intercepted. If there's a better/ more efficient way, would love to know.

amansaryal avatar Oct 12 '22 17:10 amansaryal