Reputation: 479
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?
Upvotes: 36
Views: 58872
Reputation: 567
To Send body in a GET request use:
@HTTP(method = "GET", path = "api/users", hasBody = true)
Observable<JobDeleteResponseModel> jobDelete(@Body UserPostRequestBody body);
UPDATE.- The code above is functional, I have it working on production with an extarnal api, you can see the real request logs above:
17:48:13.715 I --> DELETE https://api.paym[-redacted-]api.demo/calls/196547843/payments/116826176
17:48:13.716 I Content-Type: application/json; charset=UTF-8
17:48:13.717 I Content-Length: 36
17:48:13.718 I X-Api-Token: 28805ebc2f664d2a
17:48:13.718 I X-Company: all
17:48:13.719 I X-Api-My-Latitude: 37.4219983
17:48:13.719 I X-Api-My-Longitude: -122.084
17:48:13.722 I {"notes": "This payment was refunded in person with cash","paymentId":116826176}
17:48:13.727 I --> END DELETE (36-byte body)
17:48:13.936 I <-- 200 https://api.paym[-redacted-]api.demo/calls/196547843/payments/116826176 (208ms)
17:48:13.937 I date: Mon, 15 Jul 2024 22:49:23 GMT
17:48:13.937 I content-type: application/json; charset=utf-8
17:48:13.937 I cache-control: no-cache
17:48:13.938 I pragma: no-cache
17:48:13.938 I expires: -1
17:48:13.974 I "{succcess: true}"
17:48:13.985 I <-- END HTTP (61-byte body)
Upvotes: -5
Reputation: 597
GET call don’t accept body. so use Query("key")
or Path("key")
only POST calls accepts Body
@POST("api/") void getData(@Body UserPostRequestBody request)
Upvotes: 1
Reputation: 98
If the request body you need to send is not to complex you can also specify the query parameters as https://myserverdomain.com?somebody[property1]=value
specifically on retrofit you define it like this:
Observable<Response<SomeClass>> someMethod(@Query("somebody[property1]") int property1);
That will work at least if you are requesting to a rails server hope this answer helps somebody
Upvotes: 0
Reputation: 837
This error also occurs when API required @POST and you are using @GET
Upvotes: 6
Reputation: 4136
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.
Upvotes: 19
Reputation: 432
The definition of GET as explained here in this answer explains that a body isn't supposed to mean anything, so Retrofit doesn't allow you to add a body. That said, it is not true that a server HAS to follow this format. It's entirely possible for a server to have a GET endpoint that not only takes, but REQUIRES a body. It would be "bad" architecture, but it seems a bit silly for Retrofit to limit what the library can do unnecessarily.
Furthermore, the various HTTP methods have different definitions for what they are SUPPOSED to do. For example a GET gets information, a POST creates a new entry/object by providing information to the server, a PUT updates an existing entry/object etc. The problem is, the easiest way to pass complex data to the server, especially when using retrofit, is by using JSON. So the ideal way to GET information from the server while providing a complex filter would be to send a JSON body along with the GET request. Unfortunately, there is no HTTP request method that allows for this under the spec.
Upvotes: 24