Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP GET with request body RETROFIT

I am using Retrofit to make api calls in my android application. I have to submit a @Body of JSON

@GET("api/") void getData(@Body UserPostRequestBody request) 

I get error message

retrofit.RetrofitError: apiCall: Non-body HTTP method cannot contain @Body or @TypedOutput. 

Have you any idea?

like image 943
user2026760 Avatar asked Apr 23 '15 21:04

user2026760


People also ask

How do you pass body in GET request in retrofit?

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.

Can a HTTP GET request include a body?

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

How do you post raw whole JSON in the body of a retrofit request?

change your call interface @Body parameter to String, don't forget to add @Headers("Content-Type: application/json") : @Headers("Content-Type: application/json") @POST("/api/getUsers") Call<List<Users>> getUsers(@Body String rawJsonString); now you can post raw json.


1 Answers

To send data along with your Get Request you can do the following:

//sending data as a url parameter @GET("/group/{id}/users") List<User> groupList(@Path("id") int groupId); 

as said in this SO answer, Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request- Roy Fielding.

like image 176
harshitpthk Avatar answered Sep 24 '22 22:09

harshitpthk