Morty
Morty

Reputation: 3583

400 Bad Request with a $http.post that does not contains a body

I have an angular js application, and when trying to issue the following post request :

$resource('api/'+API_VERSION+'/projects/:projectId/users/:userId')
  .save(
          {
           projectId:$scope.project.id,
           userId:id
         },
         {}
         ,function(){
        // Handle callback
       });

I get a 400 bad request error. the request is handled by a spring RestController and the method looks like the following :

@RequestMapping(value = "/projects/{projectId}/users/{userID}",
    method = RequestMethod.POST,
    produces = MediaType.APPLICATION_JSON_VALUE)
  @RolesAllowed(AuthoritiesConstants.USER)
  void addUsers(@PathVariable Long projectId, @PathVariable Long userId) {
    log.debug("REST request to add admin to project");
    projectService.addUser(projectId, userId);
  }

I Checked the request that is been sent to the server, and nothing bad strikes me.

The url is correct (all parameter are of valid type), and the content type is set to Application json. Thanks in advance for any help.

Upvotes: 1

Views: 1881

Answers (1)

afraisse
afraisse

Reputation: 3552

Your API consumes JSON and returns void, so I think you should have consumes = MediaType.APPLICAT_JSON_VALUE in your @RequestMapping.

[Edit]:

Apart from the consumes annotation everything is fine with your back-end. Can you try making your post request with the following code :

$resource('api/'+API_VERSION+'/projects/:projectId/users/:userId',
    {projectId: $scope.project.id, userId: id}).$save();

or again, creating an instance of the resource :

var Project = $resource('api/'+API_VERSION+'/projects/:projectId/users/:userId',
    {projectId: $scope.project.id, userId: id});

var newProject = new Project();
newProject.$save();

And let me know if it worked ?

Upvotes: 2

Related Questions