Hanbit the Developer

[Retrofit2] Send Post Request With Empty Body 본문

Mobile/Android

[Retrofit2] Send Post Request With Empty Body

hanbikan 2021. 7. 21. 22:48

As you see in title, I'd like to say that codes in this post is for POST request with empty body, which means ... body: {}.

 

First of all, Set your api as below:

interface YourApi {
  @POST("api/YOUR_ROUTE")
      fun yourFunction(@Body body: RequestBody): Call<YourDto>
  }
}

 

You will pass RequestBody in parameter. And the RequestBody is like as follows:

 

 val body = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), "{}")

 

Finally, you create retrofit as originally you do.

 

val call = retrofit.create(YourApi::class.java).yourFunction(body)
// SKIP enqueue