What is a clean way to send a body with DELETE request?
Asked Answered
B

3

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.

Bott answered 1/3, 2013 at 13:43 Comment(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.Neocene
Im certain body is allowed on DELETEBott
+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?Biscay
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.Cramoisy
C
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);
    });
};
Courtesan answered 10/11, 2013 at 19:24 Comment(2)
nope. This doesn't. Same as $http.delete('domain/resource', {id: object.id});Dominquedominquez
ok, I edited your answer, you should add the correct header. Now it works :)Dominquedominquez
D
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.

Diwan answered 1/3, 2013 at 13:53 Comment(4)
Im trying to do this with Angular Resource docs.angularjs.org/api/ngResource.$resource, didnt make that clear in the question, sorryBott
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.Diwan
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.Cramoisy
Were you able to figure out a way to pass in the body for a delete method using he $resource service.Grisham
S
-2

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

Snips answered 5/9, 2014 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.