4

In my Angular app, I need to send an $http.delete request this route /projects/:id/activityTypes (note it does not end with an activity type id) passing a body with the following format:

[{"id": 2}]

This is to allow batch delete operations by sending multiple objects within the array.

Testing the route above with Rest Web Service Client I get a 200, OK status so the route is already implemented and working fine, I just need to implement it in my Angular app.

Now, I've checked this previous post which has a very similar question and learned that angular's $http.delete does not send a body in the request by default, but that:

[it] does allow us to supply a config object as the second parameter, through which we can get access to the 'data' property.

So I tried what was suggested and sent a config object in my request:

return $http.delete('projects/' + projectID + '/activityTypes', {data: [{id: 2}]})

However, it does not work. It seems that although I am sending this config object, I am still not sending a body.

How can I actually pass a body to my $http.delete route?

EDIT: Here is what the server is logging when I send the $http.delete request above:

enter image description here

The data appears to have been sent, but I get this console error which I don't get when using the Rest Client:

Object {data: "Json not valid to remove activity type from Project", status: 400, config: Object, statusText: "Bad Request"}
Community
  • 1
  • 1
Tiago
  • 4,233
  • 13
  • 46
  • 70
  • It would be much easier to use `$http.post` to call this bulk delete route and pass your data there. – Ignacy Kasperowicz Oct 20 '15 at 11:19
  • I can send a delete within a post? – Tiago Oct 20 '15 at 11:20
  • your log shows body , not sure why you say it doesn't. Sounds more like a problem in how you are handling it server side – charlietfl Oct 20 '15 at 11:21
  • @Tiago There is nothing about your server side application so I can only assume that it might be RoR and then just change your route to be a `post` instead of `delete` there. – Ignacy Kasperowicz Oct 20 '15 at 11:23
  • @charlietfl I'm not sure either, I should probably edit that out. So this is a server side parsing issue? Why does it give a 200 when I test with my Rest Client though? – Tiago Oct 20 '15 at 11:24
  • 200 just means the request status.... it doesn't mean logic at server did what it was supposed to – charlietfl Oct 20 '15 at 11:26
  • I understand that but if I get 200 on my Rest Client and 400 when actually using the application, then it must mean something is wrong with my code, correct? I must be sending a badly formatted JSON. Is the data key of the config object I'm passing appended to the request body? – Tiago Oct 20 '15 at 11:28
  • Depends how your rest is set up and is nothing we can tell from angular or any other client side code – charlietfl Oct 20 '15 at 11:30
  • By using that config object as stated on my question: `{data: [{id: 2}]}`, is this data information appended to the body of the http request? – Tiago Oct 21 '15 at 17:24

2 Answers2

6

try this:

$http({
            method: 'DELETE',
            url: 'projects/' + projectID + '/activityTypes',
            data: [{id: 2}]},
            headers: {
                'Content-type': 'application/json;charset=utf-8'
            });
Renato
  • 76
  • 1
  • 4
2

@Renato's approach will work, but so would the $http.delete(...) approach, with a little tweaking. By including the data element in the request options, you do pass along data to the server, and you can confirm this in your Developer's Console. However, without the appropriate Content-Type, your server may likely ignore it.

The following should work:

return $http.delete('projects/' + projectID + '/activityTypes', {data: [{id: 2}], headers: {'Content-Type': 'application/json;charset=utf-8'}})

Credit goes to @Harshit Anand for his on another SO post.

astangelo
  • 605
  • 5
  • 18