Difference between @Path and @Query in Retrofit with example URLs
Tests Retrofit URL construction. @Path fills a path template like /users/{user}/repos with "octocat" to make /users/octocat/repos. @Query appends ?q=retrofit to /search/repos. Red flag: claiming @Query changes the path.
WHAT THIS TESTS: Whether you understand the two primary mechanisms Retrofit uses to inject dynamic values into an HTTP request URL. Interviewers want to see that you know path substitution is template-based and strictly replaces tokens inside the URL path segments, while query parameters are appended to the end of the URL after a question mark and do not need matching placeholders in the template. This distinction is fundamental to correctly designing REST API interfaces in Android and Kotlin codebases.
A GOOD ANSWER COVERS: First, define @Path as an annotation that replaces a corresponding variable in the relative URL template, such as the {user} token in the string users/{user}/repos. Second, define @Query as an annotation that adds a key-value pair to the query string, producing a URL ending like search/repos?q=retrofit. Third, explicitly note that @Path requires the curly-brace placeholder to exist in the @GET, @POST, or other HTTP method path string, while @Query does not require any placeholder at all. Fourth, mention that multiple @Query parameters can be added to the same method and that null values may be omitted depending on the Retrofit configuration and converter setup. Fifth, briefly state that Retrofit handles URL encoding automatically for both annotations.
COMMON WRONG ANSWERS: Claiming that @Query also requires a curly-brace placeholder in the path template, which confuses it with @Path. Saying that @Path appends data after the question mark instead of substituting into the path. Stating that the base URL is somehow ignored or overridden when using these annotations. Forgetting that Retrofit encodes values automatically, so manually URL-encoding strings before passing them is unnecessary and often produces double-encoded output. Asserting that @Path and @Query are interchangeable, which they are not because they affect different parts of the URL.
LIKELY FOLLOW-UPS: How would you handle optional query parameters or safely omit null values? What is the difference between @Query and @QueryMap for dynamic sets of parameters? How does Retrofit handle URL encoding differently for path segments versus query parameters? Can you use both @Path and @Query in the same method signature, and in what order are they applied? What happens if you pass an empty string to a @Path parameter?
ONE CONCRETE EXAMPLE: Given a Retrofit instance built with the base URL https://api.github.com, consider an interface method annotated with @GET("users/{user}/repos") and parameter @Path("user") String user. Calling this method with the argument "octocat" generates the final URL https://api.github.com/users/octocat/repos. Now consider a second interface method annotated with @GET("search/repos") and parameter @Query("q") String query. Calling this method with the argument "retrofit" generates the final URL https://api.github.com/search/repos?q=retrofit. The first call modifies the path structure by filling in the template token, while the second call leaves the path alone and appends a query parameter to the end of the URL.
Read the original → square.github.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.