Home > Software design >  How can I use a post request with Retrofit to deliver data in parameters?
How can I use a post request with Retrofit to deliver data in parameters?

Time:07-27

I designed an API that sends data in parameters, however retrofit prevents us from sending data in parameters through a post request. Is there a way to send data in parameters through a post request with retrofit?

CodePudding user response:

Reference: https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/Field.html

@FormUrlEncoded
@POST("/")
Call<ResponseBody> example(
    @Field("name") String name,
    @Field("occupation") String occupation);

CodePudding user response:

You can make a Pojo/Data class for your desired parameters and Then you can make an object by passing all your parameter. Then you can send data through post by using @Body tag like:

 @POST("/")
 Call<ResponseType> send(@Body Data data)
  • Related