12
@PostMapping(path="/login")
public ResponseEntity<User> loginUser(@RequestBody Map<String, String> userData) throws Exception {
    return ResponseEntity.ok(userService.login(userData));
}

I have this method for the login in the UserController. The problem is when i try to make the post request for the login i get this error:

{
"timestamp": "2018-10-24T16:47:04.691+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<org.scd.model.User> org.scd.controller.UserController.loginUser(java.util.Map<java.lang.String, java.lang.String>) throws java.lang.Exception",
"path": "/users/login"
}

enter image description here

cosmos
  • 1,923
  • 2
  • 14
  • 27
Ionut
  • 131
  • 1
  • 1
  • 8

5 Answers5

24

You have to pass that as JSON in your body, if it's a POST request.

enter image description here

cosmos
  • 1,923
  • 2
  • 14
  • 27
5

This is happening because you are not passing a body to you server. As can I see in your screenshot you are passing email and password as a ResquestParam.

To handle this values, you can do the following:

@PostMapping(path="/login")
public ResponseEntity<User> loginUser(@RequestParam("email") String email, @RequestParam("password") String password) {
     //your imp
}

In order to accept an empty body you can use the required param in the RequestBody annotation:

@RequestBody(required = false)

But this will not solve your problem. Receiving as RequestParam will.

If you want to use RequestBody you should pass the email and password in the body.

L. Figueredo
  • 238
  • 2
  • 10
  • To receive RequestParam in POST you can refer to this question: https://stackoverflow.com/questions/17964841/how-to-get-param-in-method-post-spring-mvc You can also pass the data through the body, which makes sense too, since it is a post request. – L. Figueredo Oct 24 '18 at 17:07
  • The **@RequestBody** for the Type doesn't work, but actual Type parameters work? Wonder why? – Jose Mhlanga Jan 12 '20 at 23:49
  • this solution works for me, make sure on postman you're sending the post with the parameters as well – lsramos329 Oct 31 '21 at 11:11
4

I had a similar issue, was getting this error in my Spring Boot service

HttpMessageNotReadableException: Required request body is missing:...

My issue was that, when I was making requests from Postman, the "Content-Length" header was unchecked, so service was not considering the request body.

Florin D
  • 1,310
  • 12
  • 15
3

You need to send data in Body as JSON

 { "email":"email@email.com", "password":"tuffCookie"}
user7294900
  • 52,490
  • 20
  • 92
  • 189
0

If it's still not working, try adding additional information UTF-8 in Headers.

key : Content-Type value : application/json; charset=utf-8

For my case, I must adding UTF-8 in Headers.

Johan
  • 123
  • 1
  • 11