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
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.
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.