Send empty body in POST request in Retrofit
Asked Answered
S

7

7

My api expects an empty json body ({ }) when making post requests. How do I set this up in Retrofit and Jackson?

I tried passing null, and empty string, and "{}" but could not get this to work.

@POST(my/url)
Call<MyResponse> createPostRequest(@Body Object empty);

How can I set an empty JSON body?

Spew answered 15/6, 2017 at 20:2 Comment(3)
Have you tried without parameters?Glynda
Yes, it then sends a request without a body.Spew
I guess you can create some empty class, e.g. EmptyRequest with singleton instance and pass it as parameter.Glynda
G
4

Empty class will do the trick:

class EmptyRequest {
    public static final EmptyRequest INSTANCE = new EmptyRequest();
}

interface My Service {

    @POST("my/url")
    Call<MyResponse> createPostRequest(@Body EmptyRequest request);

}

myService.createPostRequest(EmptyRequest.INSTANCE);
Glynda answered 16/6, 2017 at 11:49 Comment(0)
B
11

An empty Object does it for Kotlin:

interface ApiService {
    @POST("your.url")
    fun createPostRequest(@Body body: Any = Object()): Call<YourResponseType>
}
Bandoline answered 31/1, 2020 at 9:32 Comment(0)
C
6

try this . It worked for me now.

@POST(my/url)
Call<MyResponse> createPostRequest(@Body Hashmap );

while using this method pass new HasMap as paremater

apiservice.createPostRequest(new HashMap())
Chasseur answered 14/4, 2018 at 13:15 Comment(0)
G
4

Empty class will do the trick:

class EmptyRequest {
    public static final EmptyRequest INSTANCE = new EmptyRequest();
}

interface My Service {

    @POST("my/url")
    Call<MyResponse> createPostRequest(@Body EmptyRequest request);

}

myService.createPostRequest(EmptyRequest.INSTANCE);
Glynda answered 16/6, 2017 at 11:49 Comment(0)
S
3

Old question, but I found a more suitable solution by using a okhttp3.Interceptor that adds an empty body if no body is present. This solution does not require you to add an extra parameter for an empty @Body.

Example:

Interceptor interceptor = chain -> {
    Request         oldRequest = chain.request();
    Request.Builder newRequest = chain.request().newBuilder();

    if ("POST".equals(oldRequest.method()) && (oldRequest.body() == null || oldRequest.body().contentLength() <= 0)) {
        newRequest.post(RequestBody.create(MediaType.parse("application/json"), "{}"));
    }

    return chain.proceed(newRequest.build());
};

You can then create an instance of your service like so:

OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(interceptor);

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("YourURL")
        .client(client.build())
        .build();

MyService service = retrofit.create(MyService.class);
Shutz answered 31/12, 2019 at 21:8 Comment(0)
E
2

use:

@POST("something")
Call<MyResponse> createPostRequest(@Body Object o);

then call:

createPostRequest(new Object())
Egidius answered 19/1, 2020 at 15:9 Comment(0)
E
0

Heres the answer in Kotlin:

    @POST("CountriesList")
fun getCountriesNew(@Body body: HashMap<String, String>) : Call<CountryModel>

      val call = RetrofitClient.apiInterface.getCountriesNew(HashMap())
Ellerey answered 5/2, 2022 at 22:24 Comment(0)
L
0

Following one is the easiest one I believe.

"{}".toRequestBody() // here "{}" will work like an empty json body.

// For Example:

val requestBody = "{}".toRequestBody()
val response = apiService.sendData(requestBody)
Luttrell answered 26/3 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.