Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 451
DSD TEJA Avatar asked Sep 06 '25 03:09

DSD TEJA


2 Answers

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.*

like image 84
Tomaz Mazej Avatar answered Sep 07 '25 22:09

Tomaz Mazej


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

This means your @GET or @DELETE should not have @Body parameter. You can use query type url or path type url or Query Map to fulfill your need. Else you can use other method annotation.

like image 29
Rakesh P. Avatar answered Sep 07 '25 23:09

Rakesh P.