Retrofit: Retrofit 2.0.0-beta2 removes part of baseUrl

Created on 22 Oct 2015  路  4Comments  路  Source: square/retrofit

Service from introduction

public interface GitHubService {
  @GET("/users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);
}

Retrofit from introduction

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .build();

Code above makes request as expected to https://api.github.com/users/{user}/repos

But if I initialize Retrofit with longer url

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/this/part/is/ignored")
    .build();

Retrofit makes calls not to https://api.github.com/this/part/is/ignored/users/{user}/repos but to https://api.github.com/users/{user}/repos

Most helpful comment

The interaction between these components is just like a URL in your browser and clicking a <a href=""> on the page. By putting a leading slash on the relative URL, you are telling the code which combines the two that this is an absolute path. Without a leading slash, it becomes a relative URL.

Additionally, without a trailing slash on the baseUrl, the "ignored" path part is seen as a sibling of "users" instead of as a parent. Adding a trailing slash to the base URL will correct that.

For more info see this answer I wrote: http://stackoverflow.com/a/32356916/132047 and the linked presentation.

All 4 comments

The interaction between these components is just like a URL in your browser and clicking a <a href=""> on the page. By putting a leading slash on the relative URL, you are telling the code which combines the two that this is an absolute path. Without a leading slash, it becomes a relative URL.

Additionally, without a trailing slash on the baseUrl, the "ignored" path part is seen as a sibling of "users" instead of as a parent. Adding a trailing slash to the base URL will correct that.

For more info see this answer I wrote: http://stackoverflow.com/a/32356916/132047 and the linked presentation.

Also documentation around this and forcing a trailing slash on the base URL (#1049) is coming.

But version 1.9 behaves as I expect

Yes. The rules have changed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ne1c picture Ne1c  路  3Comments

colintheshots picture colintheshots  路  3Comments

jpshelley picture jpshelley  路  4Comments

SSVerma picture SSVerma  路  3Comments

chriskessel picture chriskessel  路  3Comments