2

I want to delete the tag created using http post. here is the code that i have tried.

$http({
    method: 'DELETE',
    url: '/api/tags',
    data: [vm.tags.name]
}).success(function(data) {
    console.log('Gets DELETED');
    vm.tags.name = data;
}).error(function(data) {
    console.log('Does not get DELETED');
});

However this didn't work and it only sends an array with [null]. So is there something i don't see or understand here. I mean, if the POST works it should work the same way with DELETE, right? By the way it shows the log "Gets DELETED" but didn't do so.

Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
d8ta
  • 167
  • 2
  • 13

2 Answers2

5

I got the solution! In my case HTTP 1.1 was able to send the body, but the header was not able so use the content of JSON. So, by adding headers: {'Content-Type': 'application/json;charset=utf-8'} to the data: field of the $http.delete it worked and the array got send.

I hope this helps someone.

Thanks for the answers!

d8ta
  • 167
  • 2
  • 13
3

Take a look in this answer, if you send something in the body of DELETE is ignored.

You should send in the path like this:

current/path?id=1&id=2&id=3&id=4

It's like the GET method, you are not allowed to send anything in the body.

UPDATE

If you make a request with the URL parameters in the example, you will receive a object like this:

{ id : [1, 2, 3, 4] }
Community
  • 1
  • 1
Fabio Picheli
  • 939
  • 11
  • 27
  • but the article states that HTTP 1.1 allows this? – d8ta Oct 12 '16 at 12:07
  • ok, but lets assume that is the problem, how can i send the array in the path i tried: 'api/tags/' + [ myarray ] – d8ta Oct 12 '16 at 12:22
  • I don't think so, if you take a look in the [HTTP document for delete](https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7) you will see that only parameters mentioned is in the URI. – Fabio Picheli Oct 12 '16 at 12:22
  • If you want to receive a array of IDs for example, you can send multiple strings in the URL like in the example, I updated with a example of the final object – Fabio Picheli Oct 12 '16 at 12:28