0

I have a spring api endpoint that looks like this:

 public ResponseEntity<TestObj> getCounterProposals(
      @PathVariable Long testObjId, @RequestBody(required = false) Boolean status)

How do I send a request so that the status parameter gets filled up. My endpoint looks like this:

GET /api/test/{testId}

Right now its value is always null and the testId is populated. I send the request from Postman like this:

enter image description here

Should I wrap the Boolean into some DTO object?

Stoyan Lupov
  • 301
  • 1
  • 11
  • Keep in mind that a GET operation should not use a body since the GET method means retrieve whatever information. This link can help you to design your endpoint: https://stackoverflow.com/a/983458/1230748 – dcalap Feb 10 '20 at 13:03
  • @dcalap it is a requirement. It must not be in the query params :) – Stoyan Lupov Feb 10 '20 at 13:05
  • You need to use POST to send data in RequestBody – brijesh Feb 10 '20 at 13:06
  • @btreport my requirement explicitly states it should be a GET request and the status must not be visible within the query string – Stoyan Lupov Feb 10 '20 at 13:08
  • @dcalap thank you, I also think the requirement is invalid. I will use a query path query parameter for now! – Stoyan Lupov Feb 10 '20 at 13:10
  • Ok, you can try this : https://cleanprogrammer.net/making-get-request-with-body-using-spring-resttemplate/ – brijesh Feb 10 '20 at 13:10

2 Answers2

1

Since - as other people mentioned - having a body with a GET request is not conventional. If your requirement is to use @GetMapping with a status parameter that should not be visible in the query url, maybe it's also worth taking a look at @RequestHeaders?

For example: @RequestHeader("status") Boolean status

Ahmed Sayed
  • 869
  • 7
  • 10
0

I have this working:

@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name, @RequestBody Boolean status) {

    System.out.println(status);
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

And in Postman invoking the endpoint:

enter image description here

We are passing the @RequestParam name in the url ?name=David and the boolean status in the body.

And is working properly.

Note that the selected tab is Body and the type is JSON. (Right after GraphQL BETA) and the passed value is true. Not status - true as form-data as you were passing it in your Postman request.

dcalap
  • 900
  • 12
  • 29
  • sure, but thats a query param and not a body. It works for me like this too and thats what i decided to use. – Stoyan Lupov Feb 10 '20 at 13:36
  • No, is not a query param, it's a request body. Check the `@RequestBody Boolean status` that is passed in Body tab in postman as `true` in the example. – dcalap Feb 10 '20 at 13:38