1

I'm using Spring Boot and Spring MVC and I need to make a GET with a request body. Is this possible?

I tried this but it's not working. I get 404.

@RestController
@RequestMapping("/api")
public class HomeController {

    @GetMapping("/v1/foo")
    public ApiRes postBody(@RequestBody ApiReq apiReq) {
        ...
    }
}
elvis
  • 956
  • 9
  • 33
  • 56
  • 1
    It is possible to do so, but this is against HTTP design guidelines. If you are forced to use GET requests, I would recommend you use query parameters. Otherwise, use a POST request. – Markiian Benovskyi Mar 08 '22 at 10:52
  • https://stackoverflow.com/questions/34956899/does-spring-requestbody-support-the-get-method Please check this out – prabhu gandhi Mar 08 '22 at 10:59

1 Answers1

4

Technically it is possible but it is against the HTTP API Design Guidelines. Request-Bodys should only be used for POST or PUT.

For further information: https://swagger.io/resources/articles/best-practices-in-api-design/

  • 1
    programs like Postman can send a GET request with a body, but basically - the browser can't / won't allow it – c8999c 3f964f64 Mar 08 '22 at 12:23
  • But i read the "Spring start here" book, which mention that before 2014, http protocol spec does not allow request body for GET. But that changed in 2014. Nevertheless, when i try it out in the latest version of spring, seems like it still does not allow – Nick Wills Oct 11 '22 at 13:49