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?
As per my understanding, you are still able to send post request with empty body and receiving an error code from backend. So you check backend program not to send 404 instead accept your request. Use body: Uint8List(0), but note that then your content type is likely wrong since an empty body wouldn't have a JSON type.
You can send it with help of @FormUrlEncoded for example : @FormUrlEncoded @Headers("Content-Type: application/json") @POST("getclass/") Call<ExampleClass> getExampleClass(@Field("id") int id, @Field("name") String name); but I think your way is easiest and right one.
A POST request requires a body in which you define the data of the entity to be created. A successful POST request would be a 200 response code.
An empty Object does it for Kotlin:
interface ApiService {
@POST("your.url")
fun createPostRequest(@Body body: Any = Object()): Call<YourResponseType>
}
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With