0

I'm running into a problem with duplicate slashes and I'm wondering if there is a solution built into Retrofit.

My server provides the base URL we should be using which will look something like this: .setEndpoint(http://some.url.com/)

The server also passes down a path URI that should be appended to that endpoint (with various requests). So my server might send back /channels/videos.

This gets handed to Retrofit through the following method

@GET("/{uri}")
void GET(@Header("Authorization") String authHeader, @Path(value = "uri", encode = false) String uri,
         @Header("Cache-Control") String cacheHeaderValue, Callback<Object> callback);

This is then problematic because the URL that is getting hit with the GET method is http://some.url.com//channels/videos which will not work correctly in my situation.

I tried manually stripping out the trailing slash from my base endpoint - but I still saw a duplicate slash which I'm assuming resulted from the "/{uri}" and the /channels/videos.

I think my problem would be fixed by removing the leading slash in "/{uri}" but that isn't allowed in Retrofit. And removing the leading slash in the path URI I get back from my server isn't entirely feasible.

throw methodError("URL path \"%s\" must start with '/'.", path);

retrofit.RetrofitError: GET: URL path "{uri}" must start with '/'.

Is there another solution to this problem?

Relevant Links:

Possible duplicate but they describe it a little differently

Jake Wharton saying it should be de-duped in what I think is the situation I'm describing

Maybe what Jake Wharton was referencing

Currently unanswered issue asking Jake this same question

Community
  • 1
  • 1
Kyle Venn
  • 4,597
  • 27
  • 41
  • Why don't you remove the / in your end point URL? To something like this `http://some.url.com` – siriscac Jun 03 '15 at 15:00
  • `I tried manually stripping out the trailing slash from my base endpoint...` I also thought that would fix it, but it appears to be coming from the leading slash in both the annotation as well as the path I get from my server. – Kyle Venn Jun 03 '15 at 15:23

1 Answers1

0

This issue is fixed in Retrofit 2.0.0-beta3. It requires changing the method signature/annotations in your service class. The signature for the original GET method is now:

@GET
Call<Object> GET(@Header("Authorization") String authHeader, @Url String uri,
                 @QueryMap Map<String, String> options, @Header("Cache-Control") String cacheHeaderValue);

This is using compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

If you're still on the previous version of Retrofit, I solved the problem temporarily with the following helper (calling it on the uri passed to GET):

public static String validateUri(String uri) {
  if (uri.charAt(0) == '/') {
      uri = uri.substring(1);
  }
  return uri;
}
Kyle Venn
  • 4,597
  • 27
  • 41