Junior
Come aggiungere un valore nel metodo POST in Retrofit?
sobes.tech AI
Risposta dell'AI
Utilizzare l'annotazione @Field per inviare dati in formato chiave-valore (form-encoded) o @Body con un oggetto per inviare JSON.
Esempio con @Field:
interface ApiService {
@FormUrlEncoded
@POST("users")
suspend fun createUser(
@Field("name") name: String,
@Field("email") email: String
): ResponseBody
}
Esempio con @Body e un oggetto dati:
data class User(
val name: String,
val email: String
)
interface ApiService {
@POST("users")
suspend fun createUser(@Body user: User): ResponseBody
}