Retrofit: Path encoding

Created on 2 Oct 2015  路  5Comments  路  Source: square/retrofit

I have problem with path encoding.

I have request like this:

@GET("{url}")
Observable<Response<Void>> getItem(@Path(value = "url", encoded = true) String url);

and I need use url like
2015-10-01/560d3447c28eb5853953101b/560d3448fca6b4191105633a.jpg
but retrofit change it to
/2015-10-01%2F560d3447c28eb5853953101b%2F560d3448fca6b4191105633a.jpg.
It works in beta1 but it doesn't work in beta2.

Most helpful comment

Hi all,

As Jake has mentioned on my PR, it would be better to model the Service as:

@GET
Observable<Response<Void>> getItem(@Url String url);

This should solve your issue.

All 5 comments

I got the same issue passing from 2.0.0-beta1 to 2.0.0-beta2, '/' characters are encoded even if I set encoded = true in path annotation.
Algorithm has changed in RequestBuilder, alreadyEncoded is not read due to OR for '/' character.

if (codePoint < 0x20 || codePoint >= 0x7f
          || PATH_SEGMENT_ENCODE_SET.indexOf(codePoint) != -1
          || (codePoint == '%' && !alreadyEncoded))

Hi all,

As Jake has mentioned on my PR, it would be better to model the Service as:

@GET
Observable<Response<Void>> getItem(@Url String url);

This should solve your issue.

If you need a full path you should use as @venilnoronha said. Partial path replacements is when you should use @Path and it can either do encoding or not based on the rules that a path part must adhere to.

How to solve case with prefix containing more than one segment (ie containing '/' characters) ?

@GET("{prefix}/segment1/segment2")
Call<MyObject> getMyObject(@Path(value = "prefix", encoded = true) String prefix);

/////

getMyObject("/api/2.0");

// result
// GET http://host/%2Fapi%2F2.0/segment1/segment2 http 1.1

That should be put on the base URL passed to Retrofit.Builder, not on the individual paths.

Was this page helpful?
0 / 5 - 0 ratings