Does Spring @RequestBody support the GET method?
Asked Answered
F

3

36

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.

Forester answered 22/1, 2016 at 22:20 Comment(1)
you'll have to use the query string, or POST/PATCH/PUT/DELETEExstipulate
R
52

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.

Riant answered 22/1, 2016 at 22:27 Comment(8)
Which spec? Based on the link (#978561) "HTTP/1.1 spec" is now obsolete.Bodi
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.)Hibernicism
POST method is not just for changes like add / delete etc.. ?Stavros
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.Alexio
spring v5.3.6 supports request body with GET.Farkas
@BinitaBharati official doc has this kind of statement? or you have tried v5.3.6 out and come to this conclusion?Palmette
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.Farkas
In the future maye http QUERY method will be supported ietf.org/archive/id/…Cassiecassil
T
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);
}
Tafilelt answered 4/8, 2021 at 13:13 Comment(1)
This doesn't work, requestBody isn't retrieved, it is null value.Lazar
R
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

Rule answered 9/11, 2022 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.