Sobes.tech
Junior

What needs to be done to describe a GET request?

sobes.tech AI

Answer from AI

To describe a GET request when using, for example, Retrofit, you will need:

  1. Define the API interface: create a Kotlin/Java interface containing methods for interacting with the API.

  2. Use the @GET annotation: annotate the method in the interface with the appropriate annotation indicating a GET request.

  3. Specify the resource path: as a parameter of the @GET annotation, specify the relative path to the requested resource.

  4. Describe request parameters (if necessary): use @Query or @Path annotations to pass parameters in the URL.

  5. Define the return type: specify the format in which you expect to receive the response from the server (e.g., Call<YourDataType>).

// API interface
interface ApiService {

    // Description of GET request to retrieve a list of users
    @GET("users")
    fun getUsers(): Call<List<User>> // Expecting a list of User objects

    // Description of GET request with a path parameter
    @GET("users/{id}")
    fun getUserById(@Path("id") userId: Int): Call<User> // Expecting a single User object, id passed in the path

    // Description of GET request with a query parameter
    @GET("users")
    fun getUsersWithQuery(@Query("sort") sortOrder: String): Call<List<User>> // Expecting a list of User, sort parameter passed in the query
}

// Example data class (DTO)
data class User(
    val id: Int,
    val name: String
)

After defining the interface, it can be used to create a Retrofit instance and perform requests.