Endpoint throwing "Required request body is missing: public boolean" when boolean not requested

Viewed 441

The following is a generic "Send an email to a user" endpoint, in a java application with SpringBoot framework.

enter image description here

Hitting it manually with Postman works as intended, but hitting it from the mobile frontend (largely react JS based) with the following JSON:

{recipientAddress: 'sample@gmail.com', subjectLine: 'a', messageBody: 's'}  

Throws the following error:

Required request body is missing: public boolean 
    [. . .].Controllers.API.ApplicationUserController.sendEmail([. . .].Models.DTOs.EmailDTO)

I don't know where this would be coming from, as the RequestBody does not have a boolean, and the DTO it does request also does not have a boolean anywhere in it. The only boolean involved is the return value, but that wouldn't be relevant to the exception in question.

2 Answers

you should not use GET with payload as {recipientAddress: 'sample@gmail.com',...}

you can either:

  1. change @GetMapping("/SendEmail") to @PostMapping("/SendEmail") and send an object (don't forget content-type header)
curl -X POST http://localhost:8080/SendEmail -d '{"recipientAddress": "sample@gmail.com", "subjectLine": "a", "messageBody": "s"}' -H 'Content-Type: application/json'

(on postman make sure to change http method to POST)

  1. keep @GetMapping but break you payload into query params:
curl "http://localhost:8080/SendEmail?recipientAddress=sample@gmail.com&subjectLine=a&messageBody=s"

(assuming localhost:8080)

last you did not specify if you use class level annotation @RestController or @Controller, if you use @Controller - you should also add @ResponseBody on method level (use @RestController if you can)

Remove @RequestBody. If your DTO will look like this

public class EmailDTO{
  private String recipientAddress;
  private String subjectLine;
  private String messageBody;
  //add getters and setters (MANDATORY)
}

and if you send all your params as query params, it will work

if you are indeed need to sent JSON than probably you wanted @PostMapping not @GetMapping

Related