Sobes.tech
Junior — Middle

Can you list and explain the main annotations used in Retrofit for defining requests?

sobes.tech AI

Answer from AI

In Retrofit, the main annotations for defining HTTP requests include:

  • @GET — for GET requests, used to retrieve data.
  • @POST — for POST requests, typically to send data to the server.
  • @PUT — for PUT requests, often to update data.
  • @DELETE — for DELETE requests, to delete resources.
  • @PATCH — for partial updates of a resource.

Additional annotations for parameters:

  • @Path — inserts a value into the URL path.
  • @Query — adds a parameter to the query string.
  • @Body — passes an object in the request body.
  • @Header — adds an HTTP header.
  • @Field — for sending form data (with @FormUrlEncoded).
  • @Part — for sending parts of a multipart request (with @Multipart).

Example:

@GET("users/{id}")
Call<User> getUser(@Path("id") int userId, @Query("details") boolean details);

@POST("users/new")
Call<ResponseBody> createUser(@Body User user);