Thrakbad Thrakbad - 11 months ago 62
Android Question

Retrofit path replacements: replacement over whole path (including /)

In my setup, I get all the paths for my resources from the REST API from an initial call to the API. We use this pattern to be able to change all the resource paths without breaking all existing app versions in the process.

I have been playing around with Retrofit and I tried to create a method that would accept any path I pass to it as a string. My try looks like this

public FooBar getFooBar(@Path("path") String path);

I then try to call it as follows.

String path = "foo/bar";

Unfortunately Retrofit URL-Encodes the path replacement and I end up making a request to
instead of
. Is there any way to disable URL-Encoding for path replacements or to make replacements spanning multiple path segments? Unfortunately I don't even know how many path segments there are, it is all controlled by the API.

Answer Source

Use @EncodedPath! That's it. I'll copy the Javadoc so this answer has more meat:

Named replacement in the URL path. Values are converted to string using String.valueOf(Object). Values are used literally without URL encoding. See @Path for URL encoding equivalent.

Use it like this:

void example(@EncodedPath("path") String path, ..);