24

My rest api accpets DELETE requests to the following url

/api/users/{slug}

So by sending delete to a specified user (slug) the user would be deleted. here is the service code:

angular.module('UserService',['ngResource']).factory('User', function($resource){
    var User = $resource('/api/users/:id1/:action/:id2', //add param to the url
    {}, 
    { 
        delete_user: {
            method: 'DELETE',
            params: {
                id1:"@id"
            }
        },
        update: {
            method: 'PUT',
            params: {
                id1:"@id"
            }
        }
    }); 

    return User;
}); 

I call the delete function via

user.$delete_user({id:user.id}, function(){}, function(response){}); 

However the request seems to be send to the wrong url.

/api/users?id=4

So the parameter is actually missing, as a result I get a 405 Method not allowed. Is there any chance to send the delete request in the style of my api?

4 Answers 4

30

params is an object of default request parameteres in your actions. If you want url parameters you have to specify them in the second parameter like this:

angular.module('UserService',['ngResource']).factory('User', function($resource){
    var User = $resource('/api/users/:id1/:action/:id2', //add param to the url
    {id1:'@id'},
    { 
        delete_user: {
            method: 'DELETE'
        }
    }); 

    return User;
}); 

this works with either:

// user has id
user.$delete_user(function(){
  //success
},function(){
  // error
});

or

var data = {id:'id_from_data'};
User.delete_user({},data);

or

var params = {id1:'id1_from_params'};
User.delete_user(params);

I've made a plnkr-example - you have to open your console to verify that the DELETE requests are correct.

See parameterDefaults in the Angular resource documentation.

Sign up to request clarification or add additional context in comments.

8 Comments

I think this is not the problem, I added update function to my question, this works just fine, what you mention are the defaults, they get overriden anyways. However I tried you suggestion and it results in the same problem.
Yes, params in the actions are default parameteres, but not with the '@' keyword. Did you even try my suggestion - because I'm pretty sure it works. If you want to map {id:123} to id1 in /api/users/:id1 you have to have {id1:'@id'} in paramDefaults, not in the action params
If I use update the route is: /api/users/4?id=4 and it should look exactly the same for delete
I updated my answer and added a working plnkr: plnkr.co/edit/xy366u9Bm9ksXFG33E0L?p=preview
Ok this works now, but my other api method "create" does not work, the id is appended to the route, but it should not. How to fix that?
|
3

I had this problem for a while I was using a service to add / delete / update categories. While passing in params for get it worked fine but then when deleting it was giving me a ?id=1234 instead of api/resource/1234

I got around this by making the default param a string.

///Controller

Service.delete({categoryId:id}, function(resp){
  console.log(resp)//whatever logic you want in here 
});

//SERVICES

$resource('api/resource/:categoryId', {"categoryId":"@categoryId"}, {
   query:{method:"GET"},
   delete:{method:"DELETE"},

});

Should work and the resulting url will be, originally I had categoryId in the default params as a variable name.

api/resource/1234 etc

Comments

0

Just omit the '@' in the parameter

   .factory('reportFactory', ['$resource', 'baseUrl', function ($resource, baseUrl) {
        return $resource(baseUrl + '/keys/:id', {}, {
        delete: { method: 'DELETE',
            headers: {
                'Content-Type': 'application/json'
            },
            params: {id: 'id'} }
       })
}]);

this will give you:

  http://localhost:8080/reports/api/keys/b8a8a8e39a8f55da94fdbe6c 

without the question mark

1 Comment

I think you need '@id' in your params to take the id from the resource instance.
0

If you want to delete a model, there's no need to add params (params does not work for DELETE anyway):

$resource('/users/:id').delete({id: user.id}, function(res) {
  ...
})

or

$resource('/users/:role/:id').delete({role: 'visitor', id: user.id});

I'm not sure if it's a bug of ngResource.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.