How to query a param in the middle of an URL with Retrofit

3.5k views Asked by At

I'm reading post and docs about Retrofit 1 & 2. I have the next source code to get a repo from an user.

@GET("users/{user}/repos")
Call<List<GithubRepo>> getRepos(@Path("user") String user);

In retrofit2 I see that now we need to change @Path with @Query, but I don't know if the using method is the same. It's like the next one or I need to change something more?

@GET("users/{user}/repos")
Call<List<GithubRepo>> getRepos(@Query("user") String user);

Thank you

2

There are 2 answers

4
sushildlh On BEST ANSWER

both are different @Query is used

when you have to assign some value in

URL like www.xxx.com/user=name (mostly @query is used to search the user details )

we use like this ....

@GET("users/repos")
Call<List<GithubRepo>> getRepos(@Query("user") String user);

and @path is used when you change the path or URL or keyword of URL

like www.xxx.com/sam ,www.xxx.com/sushan ,etc (mostly @path is used to fetch data of different user)

we use like this ....

@GET("users/{user}/repos")
Call<List<GithubRepo>> getRepos(@Path("user") String user); //here url changes with the value of String user

NOTE:- @Query always come at end of the URL . And @Path is used anywhere in the URL

0
Payam ِDarabi On

Query parameters can also be added.

@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);

Nothing must change.