http request headers for delete

3,087 views
Skip to first unread message

jomikr

unread,
Jul 30, 2012, 8:58:07 AM7/30/12
to ang...@googlegroups.com
I use $resource to send requests to my REST backend. But I have problem sending DELETE requests with the correct header attribute 'Content-Type'.

$resource doesn't let me specify headers (until https://github.com/angular/angular.js/issues/736 is implemented), but usually it sends data per default as application/json doesn't it? For GET, POST and PUT requests at least it works. But for the following action it doesn't:

$ressource('url').delete([], data);

It sends a DELETE request with the following headers:

  1. Accept:
    application/json, text/plain, */*
  2. Accept-Charset:
    ISO-8859-1,utf-8;q=0.7,*;q=0.3
  3. Accept-Encoding:
    gzip,deflate,sdch
  4. Accept-Language:
    de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
  5. Connection:
    keep-alive
  6. Content-Length:
    137
  7. Content-Type:
    application/xml
  8. Host:
    localhost:8080
  9. Origin:
  10. Referer:
  11. User-Agent:
    Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
  12. X-Requested-With:
    XMLHttpRequest


The important thing is the Content-Type (marked red). As it works with the POST requests flawless, I'm not sure, if that is a bug or it is supposed to work this way? I now just send all the requests with the $http object, but I've hoped there is a better way.

Any clues on that?

Many thanks,
Joel

Sakae Nakajima

unread,
Aug 3, 2012, 8:45:26 PM8/3/12
to ang...@googlegroups.com
I am having the same issue, I don't understand this inconsistency.
Please let me know how to walk around this one.

Regards,

miro...@gmail.com

unread,
Jan 2, 2013, 7:24:48 AM1/2/13
to ang...@googlegroups.com
Hi! I ran into the same problem and would like to share my workaround. For $httpProvider you can set default headers. Unfortunately there are no headers for "delete". So what you can do is to override common:

window.mymodule = angular.module('mymodule', []);
mymodule.config(function ($httpProvider) {
        //copy the Content-Type header from POST to DELETE
        $httpProvider.defaults.headers.common['Content-Type'] = $httpProvider.defaults.headers.post['Content-Type']
})


Cheers

Jaswant Ghuraiya

unread,
Mar 13, 2013, 5:03:47 AM3/13/13
to ang...@googlegroups.com, miro...@gmail.com
Even after setting the content-type header for delete method, it is not reflecting in the request. The actual request header always use content-type as text/plain. This can be a bug in angular.

zhaokun li

unread,
Apr 28, 2013, 11:29:35 AM4/28/13
to ang...@googlegroups.com
I am also met the same issue. Even after using $http.delete, the header content type is still application/xml.
repro code:  $http.delete(url).success().error()

在 2012年7月30日星期一UTC+8下午8时58分07秒,jomikr写道:

zhaokun li

unread,
Apr 28, 2013, 11:42:06 AM4/28/13
to ang...@googlegroups.com
Sorry, $http works after setting header like this:
 $http.delete({url:{},headers:{'Content-Type':'application/json'}})......

在 2013年4月28日星期日UTC+8下午11时29分35秒,zhaokun li写道:

zhaokun li

unread,
Apr 28, 2013, 12:07:53 PM4/28/13
to ang...@googlegroups.com

Even after setting the content-type , it still doesn't work 
Sample code as below:
 $http({method:'DELETE', url:'somelink',headers:{'Content-Type':'application/json'} }) .success.......
And in the module config, setting $httpProvider still don't work:
 $httpProvider.defaults.headers.common['Content-Type'] = "application/json";
Anyone can give some work around? really appreciate

Saulo Araujo

unread,
May 7, 2013, 4:29:20 PM5/7/13
to ang...@googlegroups.com
I believe i have found the reason of this bug and how to work around it.

First, the reason. In the function $http(config) there is a piece of code that removes the Content-Type header when there is no data to be sent on the request:

      // strip content-type if data is undefined
      if (isUndefined(config.data)) {
        delete reqHeaders['Content-Type'];
      }

Thats why delete requests without data are being sent with the application/xml content type. When angular removes this header the browser resorts to the default content type of ajax requests: application/xml.

The work around:

just add data: null to your config map. For example

$http({
  method: "DELETE",
  url: "localhost",
  data: null
});

Hope this helps,
Saulo Medeiros de Araujo
Reply all
Reply to author
Forward
0 new messages