Retrofit & REST Quiz
DATA › Networking
With baseUrl "https://api.example.com/v1/", what URL does @GET("/users") resolve to?
- https://api.example.com/v1/users
- https://api.example.com/users
- https://api.example.com/v1//users
- It fails to compile
Answer: https://api.example.com/users
A leading slash makes the path absolute relative to the host, discarding the /v1/ segment; drop the leading slash ("users") to keep the v1 prefix.
A suspend function is declared suspend fun getUser(): User and the server returns HTTP 404. What happens?
- getUser() returns null
- getUser() returns an empty User
- Retrofit throws HttpException
- Retrofit returns a Response with isSuccessful = false
Answer: Retrofit throws HttpException
When you return the body type directly, Retrofit throws HttpException for non-2xx responses; declare the return type as Response<User> to inspect errors without an exception.
Which is the correct way to register the kotlinx.serialization converter?
- addConverterFactory(KotlinxSerializationConverterFactory.create()) for Retrofit
- addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
- addConverterFactory(MoshiConverterFactory.create()) for JSON from Moshi models
- addConverterFactory(GsonConverterFactory.create()) for Gson-backed JSON parsing
Answer: addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
kotlinx.serialization uses Json.asConverterFactory(mediaType) from the retrofit2-kotlinx-serialization-converter artifact, and the model classes must be @Serializable. The other factories belong to Moshi and Gson.
Which annotation pair correctly sends a Kotlin data class serialized as a JSON request body in a POST?
- @POST + @Body
- @POST + @Field
- @POST + @Query
- @POST + @Path
Answer: @POST + @Body
@Body passes a single object to the converter to serialize as the request body. @Field is for form-encoded data, while @Query and @Path manipulate the URL, not the body.
Which HTTP method is NOT safe to blindly retry after a transient network failure?
- GET
- PUT
- POST
- DELETE
Answer: POST
POST is not idempotent, so retrying can create duplicate resources. GET, PUT, and DELETE are idempotent and produce the same end state when repeated.
Which response should a retry policy treat as retryable rather than a permanent failure?
- 400 Bad Request, due to a malformed or invalid request
- 401 Unauthorized, when credentials are missing or invalid
- 404 Not Found, when the requested resource does not exist
- 503 Service Unavailable, a transient server-side error
Answer: 503 Service Unavailable, a transient server-side error
503 (like 408, 429, and other 5xx) is a transient server condition worth retrying with backoff. The 4xx client errors will not succeed on retry without changing the request.
Given a Response<T>, how do you correctly detect a successful HTTP response?
- response.body() != null
- response.isSuccessful
- response.code() == 200 only
- response.errorBody() == null
Answer: response.isSuccessful
isSuccessful is true for any 2xx code (200-299), the canonical success check. Checking only code()==200 misses 201/204, and body() can be null even on a successful 204 response.
Which method/parameter annotation combination sends an application/x-www-form-urlencoded request body?
- @Multipart on the method with @Part parameters for uploads
- @Body with a Map<String, String> parameter as JSON payload
- @FormUrlEncoded on the method with @Field parameters
- @GET with @Query parameters added to the URL, not the body
Answer: @FormUrlEncoded on the method with @Field parameters
@FormUrlEncoded marks the method as form-encoded and each @Field becomes a name=value pair in the body. @Multipart/@Part is for file uploads and @Query only touches the URL.
To upload a file as multipart/form-data, which Retrofit annotation setup is correct?
- @Multipart on the method with a @Part MultipartBody.Part
- @FormUrlEncoded on the method with several @Field parameters
- A single @Body parameter carrying a raw File object
- @Streaming on the method combined with @Query parameters
Answer: @Multipart on the method with a @Part MultipartBody.Part
Multipart uploads require @Multipart on the method plus @Part parameters, typically a MultipartBody.Part built from a RequestBody. @FormUrlEncoded and @Body cannot produce a multipart body.
An API returns an absolute next-page link and you want to request it directly. Which parameter annotation lets a method take a full URL that overrides baseUrl?
- @Path
- @Query
- @Header
- @Url
Answer: @Url
A String/HttpUrl parameter annotated @Url is used as the request URL, resolved against baseUrl, so passing an absolute URL bypasses the configured base path entirely. @Path only substitutes a single segment.
When a server responds 429 Too Many Requests, which response header should a well-behaved retry policy honor to decide how long to wait?
- Cache-Control
- Retry-After
- ETag
- Content-Type
Answer: Retry-After
Retry-After tells the client how many seconds (or until what date) to wait before retrying, and servers send it on 429 and 503. Honoring it avoids hammering an overloaded or rate-limiting backend.
Every request must carry an Authorization bearer token that can be refreshed at runtime. What is the cleanest wiring in Retrofit/OkHttp?
- Bake the auth token directly into the baseUrl string
- Add an @Field("token") parameter onto every single endpoint
- Add an OkHttp Interceptor that sets the header per request
- Override toString() on the Retrofit service interface
Answer: Add an OkHttp Interceptor that sets the header per request
An application Interceptor can read the current token and add the Authorization header to every request in one place, so refreshes take effect without editing each endpoint. (An Authenticator additionally handles 401 re-auth.)
What does annotating a download method with @Streaming accomplish?
- It returns the ResponseBody stream without buffering it all
- It automatically retries the request after any I/O failure occurs
- It gzip-compresses the response on the device before delivery
- It parses the body as JSON before handing it back to you
Answer: It returns the ResponseBody stream without buffering it all
@Streaming delivers the ResponseBody as a stream so you can read large payloads (e.g. file downloads) incrementally instead of loading the entire response into memory. Without it Retrofit buffers the full body first.
In REST semantics, which pair of HTTP methods is considered 'safe' (read-only, causing no server-state change)?
- POST and PUT
- PUT and DELETE
- POST and PATCH
- GET and HEAD
Answer: GET and HEAD
Safe methods are read-only by definition: GET and HEAD (also OPTIONS/TRACE) must not alter server state. PUT and DELETE are idempotent but not safe, and POST is neither safe nor idempotent.