35

I am trying to carry JSON data in an HTTP GET request message, but my Spring MVC server can't seem to retrieve the JSON data from the GET request body.

Mario Cervera
  • 671
  • 1
  • 8
  • 19
Hongbo Tian
  • 363
  • 1
  • 3
  • 5

3 Answers3

46

HTTP's GET method does not include a request body as part of the spec. Spring MVC respects the HTTP specs. Specifically, servers are allowed to discard the body. The request URI should contain everything needed to formulate the response.

If you need a request body, change the request type to POST, which does include the request body.

yxre
  • 3,576
  • 21
  • 20
  • 7
    Which spec? Based on the link (https://stackoverflow.com/questions/978061/http-get-with-request-body#comment68112820_983458) "HTTP/1.1 spec" is now obsolete. – skryvets Jun 11 '19 at 16:22
  • 5
    Exactly. The HTTP specs nowadays says nothing of the sort (other than the GET request body semantics are independent of the GET request processing semantics.) – luis.espinal Oct 10 '19 at 14:39
  • 4
    POST method is not just for changes like add / delete etc.. ? – Adir Dayan Oct 18 '20 at 13:40
  • 8
    Indeed as you have pointed out Spring must be discarding the body for GET call. I just want to further discuss -- The problem with using POST, just for the sake of including a requestbody is that - we are sort of changing the meaning of the call. As in, All is want to do is "get" a resource and i want to specify additional criteria in request body. When I use a post call, it appears as though I want to save something, but I am actually not saving anything. That is why I feel that RequestBody should be supported for GET calls. Please correct me if I am missing anything here. Thanks. – Ashish Mishra Apr 30 '21 at 14:24
  • 6
    spring `v5.3.6` supports request body with GET. – Binita Bharati Jan 18 '22 at 08:24
  • 1
    @BinitaBharati official doc has this kind of statement? or you have tried v5.3.6 out and come to this conclusion? – niaomingjian Jan 18 '22 at 09:38
  • I tried it out. So, both my web server and spring allowed the request body to get through. But, seemingly HTTP specs said nothing about this particular case, so , web server implementations may vary. Some may drop the request body itself. – Binita Bharati Jan 18 '22 at 11:11
  • In the future maye http QUERY method will be supported https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-02.html – Milan Majer Sep 16 '22 at 08:48
0

Based on official info https://docs.spring.io/spring-framework/docs/4.1.0.RC2/spring-framework-reference/html/mvc.html

@RequestMapping("/something")
public ResponseEntity<String> handle(HttpEntity<byte[]> requestEntity) throws UnsupportedEncodingException {
    String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader"));
    byte[] requestBody = requestEntity.getBody();
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("MyResponseHeader", "MyValue");
    return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
Stanley
  • 801
  • 2
  • 10
  • 20
0

In case anyone's here trying to get the OpenAPI generation to treat the fields of the request object as separate GET params, you'll want to use @ParameterObject (org.springdoc.api.annotations.ParameterObject) which was added here: https://github.com/springdoc/springdoc-openapi/issues/590

mowwwalker
  • 16,634
  • 25
  • 104
  • 157