Spring post method "Required request body is missing"
Asked Answered
S

10

26
@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

Starobin answered 24/10, 2018 at 16:55 Comment(3)
Hi! what is the body you are passing to your API?Reserpine
Do i need to pass the email and password in Body?Starobin
Depends, you can do on both ways, se my anwer :DReserpine
C
40

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

enter image description here

Cristicristian answered 24/10, 2018 at 17:6 Comment(2)
Change Media type Text to JSON as I have done in the picture above.Cristicristian
for some case (happened to me), it must be added headers information.Sevenup
H
16

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.

Hurless answered 1/10, 2021 at 20:6 Comment(0)
R
9

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.

Reserpine answered 24/10, 2018 at 17:2 Comment(3)
To receive RequestParam in POST you can refer to this question: #17965341 You can also pass the data through the body, which makes sense too, since it is a post request.Reserpine
The @RequestBody for the Type doesn't work, but actual Type parameters work? Wonder why?Vikkivikky
this solution works for me, make sure on postman you're sending the post with the parameters as wellGaffrigged
S
4

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.

Sevenup answered 27/4, 2022 at 8:12 Comment(1)
Same case with meBoanerges
S
3

You need to send data in Body as JSON

 { "email":"[email protected]", "password":"tuffCookie"}
Starlet answered 24/10, 2018 at 17:5 Comment(2)
@lonut click rawStarlet
@lonut click on text combo and change to jsonStarlet
M
0

In my case it was poorly defined JSON that I sent to my REST service.

Attribute that was suppose to be an object, was in my case just string:

Changed from:

"client" = "",

to:

"client" = { ... },

In my case String did not add additional information about value in different format.

Mortgage answered 13/7, 2022 at 15:41 Comment(0)
C
0

In my case, adding such header 'Content-Type: application/x-www-form-urlencoded' solves the issue. The servlet do have the @RequestBody part. I assume Spring will need such header to form the post body

Chucklehead answered 27/6, 2023 at 7:19 Comment(0)
S
0

If it is XML Request Try importing jackson-dataformat-xml.jar to your project it worked for me.

Salicaceous answered 11/7, 2023 at 4:18 Comment(0)
S
0

in my case i was using @RequestBody in a @GetMapping api and for that reason happened to face the issue "Required request body is missing", hope nobody do this crazy thing because doesn't make sense at all but if someone like will do, here is a quick solution to fix it :D

Shurwood answered 3/11, 2023 at 10:34 Comment(0)
L
0

I was encountering the same issue, turns out that my JSON body was not valid. I mindlessly added some Java style comments which made it invalid.

Lynnlynna answered 17/11, 2023 at 20:47 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Protasis

© 2022 - 2024 — McMap. All rights reserved.