How to send request body in get method retrofit

I am using the retrofit library for network calls. In this, I need to pass Body in GET Method. But I am getting the error while I am passing this one. In Postman it is working while passing Body for GET Method.

@GET("http://192.168.0.141:3000/api/contacts/{page_num}/{limit}")
fun getAllContacts(@Path("page_num") page_num:Int,@Path("limit") limit:Int,@Body reqBody:ContactsInpRequest):Call<AllContactsDataResponse>

I am calling get method by passing body. But I am getting the below exception.

java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.

Answer

GET method does not contain body like the POST does. Here you can learn more about REST methods: https://restfulapi.net/http-methods/

EDIT: I see that you said that it works in Postman so take a look at this:

*CAN GET request have a body?

In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. … Yes, you can send a request body with GET but it should not have any meaning.*

Leave a Reply

Your email address will not be published. Required fields are marked *