23

hye,

i am building an app with angular.js and node.js (Express.js) on the server side.

for some reason i am having a problem handling a delete request. no body is getting to the server side.

this is my angular.js resource code:

$scope.deleteProject = function(projectName){
    var postData = {username: 'name', projectName: projectName};
    Project.deleteProject.delete({}, postData,
        function(res){
            alert('Project Deleted');
        },
        function(err){
            alert(err.data);
    });
}

on the server side i have this:

var deleteProject = function(req, res){
    console.log(req.body);
    console.log(req.params);
    if (req.body.projectName){
        //do something
        return res.send(200);
    }
    else
        return res.send(400, 'no project name was specified');
}

now for some reason there is no body at all!! it is empty. i have defined the route as app.delete.

if i change the route in node.js to post and in angular.js to save it works fine.

what am i missing here (banging my head).

thanks.

lobengula3rd
  • 1,821
  • 4
  • 26
  • 39
  • Possible duplicate of [What is a clean way to send a body with DELETE request?](http://stackoverflow.com/questions/15159213/what-is-a-clean-way-to-send-a-body-with-delete-request) – RMorrisey Apr 21 '16 at 20:34

6 Answers6

39

As per this stack overflow question and the $http service source code, a DELETE request using $http does not allow for data to be sent in the body of the request. The spec for a DELETE request is somewhat vague on whether or not a request body should be allowed, but Angular does not support it.

The only methods that allow for request bodies are POST, PUT, and PATCH. So the problem is not anywhere in your code, its in Angular's $http service.

My suggestion would be to use the generic $http(...) function and pass in the proper method:

$http({
    method: 'DELETE',
    url: '/some/url',
    data: {...},
    headers: {'Content-Type': 'application/json;charset=utf-8'}
})
Community
  • 1
  • 1
tennisgent
  • 14,165
  • 9
  • 50
  • 48
  • why put? it seems very wrong to make a put request for a deletion. maybe post? – lobengula3rd Mar 05 '14 at 07:43
  • 4
    PUT is generally used for a "modification" of the existing record. POST is typically used for "creation". It doesn't really matter which one you use, so its really up to you. – tennisgent Mar 05 '14 at 14:17
  • Is there any reason why all the http service codes do not support having a body attached? Currently I'm mainly using the service codes for semantic purposes. – Ouwen Huang Nov 29 '14 at 06:50
  • If it were up to me, I would have allowed a request body for all request types, but the Angular team for some reason decided not to support it for deletes. Not sure why. I don't know that there is really a reason. – tennisgent Dec 01 '14 at 04:08
  • 5
    When you say that `$http` doesn't support body on DELETE you should state that you're referring to `$http.delete()` function. If you used simply `$http(config)` it's totally possible to send message body even with deletes... – Robert Koritnik Jan 07 '15 at 07:52
37

Angular by default sends the Content-Type as text/plain for DELETE requests. Just add this to the headers:

  var config = {
    method: "DELETE"
    url: yourUrl
    data: yourData
    headers: {"Content-Type": "application/json;charset=utf-8"}
  };

  $http(config);

If you want to add them to every single DELETE request add this to the app.config method in your main controller:

 $httpProvider.defaults.headers.delete = { "Content-Type": "application/json;charset=utf-8" };
jtello
  • 451
  • 4
  • 7
3

If you want to use the $resource object instead of $http you need to add hasBody and headers as follow:

delete:  {
    method: 'DELETE',
    hasBody: true,
    headers: {"Content-Type": "application/json;charset=UTF-8"}
  }

Worked for me

shgutman
  • 134
  • 1
  • 5
  • 1
    hasBody was added in angular 1.6.4, so if you're using an earlier version this will not work. – tbm Oct 08 '17 at 17:11
1

Just ran into this problem. You'll have to use url params to send an id with delete.

in express:

app.delete('/api/user/:userId', user.remove);

and add to the url in angular:

$http({url: 'whatever/api/'+obj.id, method: 'DELETE'}) ...
Corey Rothwell
  • 394
  • 2
  • 9
0

The following works for me:

$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8';

XMLHttpRequest is optional but useful if you are sending ajax. https://docs.angularjs.org/api/ng/provider/$httpProvider for more information.

Tigertron
  • 628
  • 6
  • 6
0

This worked for me.

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

And then

$http.delete(url, { data: data })
Diego Mello
  • 5,220
  • 3
  • 17
  • 21