Ionut
Ionut

Reputation: 281

Spring post method "Required request body is missing"

@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

Upvotes: 27

Views: 219436

Answers (10)

matthewcherrey
matthewcherrey

Reputation: 5

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.

Upvotes: 0

Lisen Saka
Lisen Saka

Reputation: 56

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

Upvotes: 0

srivaishnavi k
srivaishnavi k

Reputation: 1

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

Upvotes: 0

rosa
rosa

Reputation: 107

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

Upvotes: 0

Viktor Reinok
Viktor Reinok

Reputation: 130

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.

Upvotes: 0

Johan
Johan

Reputation: 435

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.

Upvotes: 4

Florin D
Florin D

Reputation: 1870

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.

Upvotes: 17

cosmos
cosmos

Reputation: 2263

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

enter image description here

Upvotes: 40

Ori Marko
Ori Marko

Reputation: 58892

You need to send data in Body as JSON

 { "email":"[email protected]", "password":"tuffCookie"}

Upvotes: 3

L. Figueredo
L. Figueredo

Reputation: 278

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.

Upvotes: 9

Related Questions