26

I need to send a request body with my DELETE requests using $resource

The only way I could see to do this was to change:

https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js

From

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';

To

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH' || action.method == 'DELETE';

Is there a better way to override this? Like when you alter the content type header you can do:

$httpProvider.defaults.headers["delete"] = {'Content-Type': 'application/json;charset=utf-8'};

Or something similar... Ive googled this but maybe Ive missed something obvious (not for the first time). Thanks for any help in advance.

4
  • I would like to point out. DELETE is supposed to delete the resource identified by the url. So you should not sending data in the body. Commented Mar 1, 2013 at 13:57
  • 1
    Im certain body is allowed on DELETE Commented Mar 1, 2013 at 13:59
  • +1, I was about to post the same exact question. @SubirKumarSao, I'm wanting to send a request body with my DELETE for deleting multiple resources (the resources to be deleted is what is in the request body). Is there a more RESTful way to do this? Commented Mar 5, 2013 at 6:40
  • 2
    I think the above comment is referring to DELETE /post being unRESTful because it should specify a specific resource in the URL. However, I'm in a situation where I want to DELETE /post/:id but I need to make sure that the user, who sends an identifier token as data, owns the post. Commented Jul 9, 2013 at 0:20

3 Answers 3

28

This works.

$scope.delete = function(object) {
    $http({
        url: 'domain/resource',
        method: 'DELETE',
        data: {
            id: object.id
        },
        headers: {
            "Content-Type": "application/json;charset=utf-8"
        }
    }).then(function(res) {
        console.log(res.data);
    }, function(error) {
        console.log(error);
    });
};
Sign up to request clarification or add additional context in comments.

2 Comments

nope. This doesn't. Same as $http.delete('domain/resource', {id: object.id});
ok, I edited your answer, you should add the correct header. Now it works :)
2

You can inject the $http (http://docs.angularjs.org/api/ng.%24http#Usage) component into one of one of your controllers and by using it as follows :

$http({method: 'DELETE', url: 'www.url.com', headers: {'X-MY-HEADER': 'MY_VALUE'}});

I hope this what you expected.

4 Comments

Im trying to do this with Angular Resource docs.angularjs.org/api/ngResource.$resource, didnt make that clear in the question, sorry
Ok. I think that $resource isn't made for such treatment, it is apparently meant to be used in a data binding context. For particular specific request, I guess that $http has to be directly used.
From what I'm reading, it's considered bad practice to add the "X-" prefix if this is a custom header. I'd still rather send content with the delete than a custom header.
Were you able to figure out a way to pass in the body for a delete method using he $resource service.
-2

You should be able to call 'remove' on your resource as explained in the documentation https://docs.angularjs.org/api/ngResource/service/$resource

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.