Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting 400 bad request with AngularJs post?

I use Spring MVC and AngularJs to create a web application. I send a request to the server but I am getting 400 error bad request. I configure the message converter for the json format in the Spring servlet configuration file. I am wondering why I am getting this error.

Here is my angular service :

save : function(user) {
    return $http({
        method: 'POST',
        url: '/app-web/user/create',
        contentType: "application/json",
        data:user
    });
}

And on the server side I have a Spring MVC Controller as described below :

@RequestMapping(value="/user/create", method= RequestMethod.POST)
@ResponseBody
public String createAccount(@RequestBody User user){
  //some logic
    return "Ok";
}

I noticed something else: when I remove the @RequestBody in the controller I don't have a 400 error but the user is null:

@RequestMapping(value="/user/create", method= RequestMethod.POST)
@ResponseBody
public String createAccount(User user){
  //some logic
    return "Ok";
}
like image 568
Pracede Avatar asked Oct 10 '14 20:10

Pracede


People also ask

What is 400 error in angular?

Check for File Size As we've mentioned earlier, another cause for a 400 error is file size. If you're trying to upload a file that's too large that it exceeds the server file limit, you'll get a 400 error. To confirm that this is causing the issue, try to upload a file that's smaller in size.

What does a 400 Bad Request mean?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

What is HTTP get in AngularJS?

AngularJS automatically injects $scope parameter at runtime. The $http. get() method returns HttpPromise which includes methods like success() and error(). The success() method registers a callback method which is called when a request completes successfully.


1 Answers

The problem was about the user form. I had lastname, firstname, email, password, password1 but the User Java Object does not contains password1 attributes. When json data provide by the request does not correspond to the Java Object, the JsonConverter is not able to match to data to Java Object.

like image 157
Pracede Avatar answered Sep 28 '22 20:09

Pracede