2

I am trying to do a get call with request body(JSON) as the request parameter list exceeds the limit. I am able to send the request via postman/insomnia and request is reaching till controller without any error. But the "requstBody" is empty at controller. What i am missing here?

@GET
@Path("\path")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response getResponse(String requestBody) throws IOException { }

When I replaced @GET with @POST, requestBody has value. For GET call do we need to add anything more?

vineeth
  • 198
  • 3
  • 13
  • I'd recommend you to take a look at: https://stackoverflow.com/a/983458/4934937 – maio290 Jan 03 '19 at 10:09
  • @maio290 The link doesn't show how to get it resolved. It is sharing the ethics of passing request body in GET calls – vineeth Jan 03 '19 at 10:41

4 Answers4

3

I am trying to do a get call with request body(JSON) as the request parameter list exceeds the limit. I am able to send the request via postman/insomnia and request is reaching till controller without any error. But the "requstBody" is empty at controller. What i am missing here?

One thing you are missing is the fact that the semantics of a request body with GET are not well defined.

RFC 7231, Section 4.3.1:

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • By rejecting request you meant rejecting requestBody right? Because I am able to hit the my controller with the request but the requestBody is empty – vineeth Jan 03 '19 at 11:09
  • @vineeth to quote from the old c++ standards newsgroup: "not defined" = "could make demons fly out of your nose". So any action the server or framework takes would be seen as ok. Be it rejecting the request, ignoring the body or using the body. – jokster Jan 03 '19 at 12:57
2

put @RequestBody on String requestBody parameter

2
@RequestMapping("/path/{requestBody}")
public Response getResponse(@PathVariable String requestBody) throws IOException { }
mkjh
  • 1,634
  • 13
  • 25
2

There are two ways for sending parameters in an Http Get method. PathVariable and RequestParam. In this way, sent parameters are visible in the request URL. for example:

www.sampleAddress.com/countries/{parameter1}/get-time?city=someValues

In the above request, parameter1 is a path variable and parameter2 is a request parameter. So an example of a valid URL would be:

www.sampleAddress.com/countries/Germany/get-time?city=berlin

To access these parameters in a java controller, you need to define a specific name for the parameters. For example the following controller will receive this type of requests:

@GetMapping(value = "/countries/{parameter1}/get-time", produces = "application/json; charset=utf-8")
public String getTimeOfCities(
    @PathVariable(value = "parameter1") String country,
    @RequestParam(value = "city") String city
){
    return "the method is not implemented yet";
}

You are able to send RequestBody through a Get request but it is not recommended according to this link.

yes, you can send a body with GET, and no, it is never useful to do so.

This elaboration in elasticsearch website is nice too:

The HTTP libraries of certain languages (notably JavaScript) don’t allow GET requests to have a request body. In fact, some users are suprised that GET requests are ever allowed to have a body.

The truth is that RFC 7231—the RFC that deals with HTTP semantics and content—does not define what should happen to a GET request with a body! As a result, some HTTP servers allow it, and some—especially caching proxies—don’t.

If you want to use Post method, you are able to have RequestBody too. In the case you want to send data by a post request, an appropriate controller would be like this:

@PostMapping(value = "/countries/{parameter1}/get-time", produces = "application/json; charset=utf-8")
public String getTimeOfCitiesByPost(
        @PathVariable(value = "parameter1") String country,
        @RequestParam(value = "city") String city,
        @RequestBody Object myCustomObject

){
    return "the method is not implemented yet";
}

myCustomObject could have any type of data you defined in your code. Note that in this way, you should send request body as a Json string.

Hamid Ghasemi
  • 880
  • 3
  • 13
  • 32
  • you can send body with GET method too, but it's ignored for most cases. – Murat Güvenç Jan 03 '19 at 11:02
  • @hamid from some other links, I can see, via GET call we can pass RequestBody too. See https://stackoverflow.com/a/983458/4934937 – vineeth Jan 03 '19 at 11:03
  • @MuratGüvenç This is not a standard way and I suggest to prevent that. – Hamid Ghasemi Jan 03 '19 at 11:04
  • absolutely it is not a standard or advised way. Prevent this kind of API design if possible. – Murat Güvenç Jan 03 '19 at 11:09
  • @vineeth I have added an important part of the link you mentioned in your comment. – Hamid Ghasemi Jan 03 '19 at 11:10
  • 1
    Some HTTP servers allow it, and some—especially caching proxies—don’t. We tried a POC with Tomcat on which we passed the requestBody successfully. But when we tried the same with Weblogic( which is a caching proxies server), we were not able to succeed. – vineeth Jan 04 '19 at 04:37