Sobes.tech
Junior

How to add a value in a POST method in Retrofit?

sobes.tech AI

Answer from AI

Use the @Field annotation to send data in key-value (form-encoded) format or @Body with an object to send JSON.

Example with @Field:

interface ApiService {
    @FormUrlEncoded
    @POST("users")
    suspend fun createUser(
        @Field("name") name: String,
        @Field("email") email: String
    ): ResponseBody
}

Example with @Body and a data object:

data class User(
    val name: String,
    val email: String
)

interface ApiService {
    @POST("users")
    suspend fun createUser(@Body user: User): ResponseBody
}