DSD TEJA
DSD TEJA

Reputation: 71

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.

Upvotes: 4

Views: 12576

Answers (3)

ALEJO MU&#209;OZ
ALEJO MU&#209;OZ

Reputation: 37

   @Headers("Content-Type: application/json")
@GET("helper-url")
fun getHelperUrl(
    @Query("api_token") apiToken: String,
    @Query("authtype") authType: String,
    @Query("channel") channel: String
): Call<ResponseHelperUrl>

Upvotes: 2

Tomaz Mazej
Tomaz Mazej

Reputation: 465

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

Upvotes: 7

Rakesh P.
Rakesh P.

Reputation: 183

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.

Upvotes: 4

Related Questions