25

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.

Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
paullth
  • 1,731
  • 2
  • 15
  • 19
  • 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. – Subir Kumar Sao Mar 01 '13 at 13:57
  • 1
    Im certain body is allowed on DELETE – paullth Mar 01 '13 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? – dnc253 Mar 05 '13 at 06: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. – rgbrgb Jul 09 '13 at 00:20

3 Answers3

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);
    });
};
Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
Simba
  • 522
  • 1
  • 5
  • 9
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.

Halim Qarroum
  • 13,985
  • 4
  • 46
  • 71
  • Im trying to do this with Angular Resource http://docs.angularjs.org/api/ngResource.$resource, didnt make that clear in the question, sorry – paullth Mar 01 '13 at 14:01
  • 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. – Halim Qarroum Mar 01 '13 at 14:35
  • 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. – rgbrgb Jul 09 '13 at 17:36
  • Were you able to figure out a way to pass in the body for a delete method using he $resource service. – AshD Oct 27 '21 at 18:07
-2

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

Votemike
  • 762
  • 1
  • 10
  • 28