Play! returning bad request 400

127 views Asked by At

I am trying to insert data into a mongo collection through play. Action in Play controller is as follows:

def create = Action.async(parse.json) { request =>
request.body.validate[Users].map {
  user=>
    collection.insert(user).map {
      lastError => Created(s"User created successfully")
    }
}.getOrElse(Future.successful(BadRequest("invalid json")))

}

And the http request from angularjs is as follows:

app.controller("userCtrl", function($scope, $http) {
$scope.AddUser = function(user){ 
    $scope.initRec=user;
    $scope.user=angular.copy(user);
    var user= $scope.user;
    console.log('POST', user);
    $http({
            method: 'POST',
            url: '/users',
            withCredentials: true,
            headers: {'Content-Type': 'application/json',
            'Access-Control-Allow-Headers': 'Accept, Origin, Content-type, 
X-Json, X-Prototype-Version, X-Requested-With',
             'Access-Control-Allow-Origin':'*'
             },
            data: user
         }).then( function(result){
                /*success or error callbacks */);
    };

}
});

Response from Play is "Bad Request or invalid json". However the request body in developer tools show {"name":"test","location":"test"}

What is wrong with this code. I am totally new to AngularJs and Play!. Please help.

0

There are 0 answers