Retrofit & REST Quiz

DATA › Networking

With baseUrl "https://api.example.com/v1/", what URL does @GET("/users") resolve to?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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)?

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.

Back to Retrofit & REST