Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Unable to set content-type request header for delete method. #2149

Closed
jaswant-ghuraiya opened this issue Mar 13, 2013 · 28 comments
Closed

Unable to set content-type request header for delete method. #2149

jaswant-ghuraiya opened this issue Mar 13, 2013 · 28 comments

Comments

@jaswant-ghuraiya
Copy link

I tried as below approach-

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

My request doesn't have any body. And the actual request header is always taking "content-type" as "text/plain".

@kdekooter
Copy link

Having similar issues with put and post. Content-Type of the request is always set as application/xml

@michelsalib
Copy link

I am having the exact same issue. Impossible to change the Content-Type of a DELETE request.

@d13r
Copy link

d13r commented Jul 20, 2013

I think if there is no request body (data parameter) the Content-Type header is deliberately removed:

if (lowercase(header) === 'content-type') {

The code above worked for me when the data parameter is set.

@arciisine
Copy link

Running into the same issue as well. When working with a framework that expects content-type for all HTTP verbs, this is an issue.

Also, since neither GET and DELETE support content, I would expect them both to work the same way.

@david-driscoll
Copy link

I wonder if there is a specific reason the content-type is stripped? JQuery doesn't make that assumption for example. This was a frustrating little feature to find out.

@Grummle
Copy link

Grummle commented Aug 14, 2013

Is DELETE supposed to have Content-Type always set?

When using $http.delete(url) we've been seeing the following:

Chrome 28 (and some lower):

Content-Type:application/xml

FireFox 22:
Content-Type text/plain; charset=utf-8

IE 10:
Content-Type text/plain;charset=UTF-8

The Content-Type header throws our conneg for a loop. Perviously using:

$.ajax({url:url,type:'DELETE'});

We didn't have this issue and no 'Content-Type' was specified.

Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:localhost
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
X-Requested-With:XMLHttpRequest

The issue seems to be with this piece of code in $httpBackend:

      xhr.send(post || '');

If you simply change that to something like:

if(post) xhr.send(post);
else xhr.send();

Then all is well with the world. I've tested it on the 1.1.5 tag and all the tests pass in Mac Chrome/Safari include e2e, but I haven't a clue how to write tests for it or I'd submit a pull request. Currently we are forced to run a modifed version of angular, but we'd like to run the google cdn version. Any suggestions?

@mikeobrien
Copy link

👍

2 similar comments
@mariussoutier
Copy link

+1

@muratcorlu
Copy link

+1

@muratcorlu
Copy link

xhr.send(post || null);

works too. I hope somebody merges this fix to master.

@eranation
Copy link

👍

1 similar comment
@srawlins
Copy link
Contributor

+1

@kdekooter
Copy link

OK. It's been 5 months now. It would be nice to get some kind of feedback from one of the core committers.

@muratcorlu
Copy link

My pull request about this issue has been included in milestone 1.2.0-rc3: #3897 I hope, it will be merged in next release.

@kdekooter
Copy link

Excellent!

@ejain
Copy link

ejain commented Sep 21, 2013

Just ran into this issue with 1.0.8; hope the fix will be included in a 1.0.9 release! Until then, it appears that AngularJS won't work with the current version of the popular Play framework (2.2)...

@mariussoutier
Copy link

@ejain Yes it does, just add parse.empty to your Actions.

@ejain
Copy link

ejain commented Sep 22, 2013

@mariussoutier Thanks for the workaround; unfortunately there does not appear to be an equivalent of parse.empty in Play's Java API.

@jroper
Copy link
Contributor

jroper commented Sep 23, 2013

PR that tests and fixes this:

#4021

btford pushed a commit to btford/angular.js that referenced this issue Oct 1, 2013
The XMLHttpRequest.send spec defines different semantics for `null`
than for an empty String: an empty String should be sent with a
`Content-Type` of `text/plain`, whereas null should have no
`Content-Type` header set.

Closes angular#2149
@btford btford closed this as completed in 0d0330a Oct 1, 2013
@johann-sonntagbauer
Copy link

one question about the changes concerning

xhr.send(post || null);

and

forEach(headers, function(value, key) {
    if (isDefined(value)) {
        xhr.setRequestHeader(key, value);
    }
});

That makes problems when an empty POST request is made in an CORS enabled environment.

Because CORS will interpret the Content-Type header and with that changes there is absolutely no way to define an Content-Type header for an empty POST.

Problem reproduced with angular 1.2.9

jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
The `XMLHttpRequest.send` spec defines different semantics for `null`
than for an empty String: an empty String should be sent with a
`Content-Type` of `text/plain`, whereas `null` should have no
`Content-Type` header set.

Closes angular#2149
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
The `XMLHttpRequest.send` spec defines different semantics for `null`
than for an empty String: an empty String should be sent with a
`Content-Type` of `text/plain`, whereas `null` should have no
`Content-Type` header set.

Closes angular#2149
@brutto
Copy link

brutto commented Jul 7, 2015

Still can not set content type with $resource on GET (empty POST have not check yet). =\

@Ravireddy1076
Copy link

I just migrated from 1.2 to 1.4. It seems the issue is back. Default Content-type set to text/* for Delete request.

@BespalovAV
Copy link

@idf
Copy link

idf commented Sep 8, 2015

+1

@aegrey
Copy link

aegrey commented Oct 27, 2015

+1

@gkalpak
Copy link
Member

gkalpak commented Oct 28, 2015

If you are still experiencing buggy behaviour, I suggest opening a new issue (providing the necessary info - preferrably along with a live demo, using CodePen, Plnkr etc).

It's hard to interpret +1s on an issue that (is supposed to) have been fixed.

@vniche
Copy link

vniche commented Dec 18, 2015

I'm having the same issue, tried to reproduce it in a Plunk (v1.4.8), but no success, and locally (also v1.4.8), i'm facing it, don't know if it makes any difference, but my request is to a rest application in a local hosted VirtualBox VM.

Punkler Request info:

POST /posts HTTP/1.1
Host: jsonplaceholder.typicode.com
Connection: keep-alive
Content-Length: 46
Accept: application/json, text/plain, */*
Origin: http://run.plnkr.co
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://run.plnkr.co/eLPXRF5pvhFsXNKK/
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4

My local Request info:

OPTIONS /zabbix/api_jsonrpc.php HTTP/1.1
Host: 192.168.22.114
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:8888
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:8888/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4

I'm using the same method locally, a lot Headers are different, i don't know why.

@gkalpak
Copy link
Member

gkalpak commented Dec 21, 2015

@vniche: This seems to be a different issue (and one not related to Angular).
Since I see that there is an OPTIONS request, I assume this is CORS related. The browser probably doesn't send a Content-Type header with the "preflight" request, since no data is sent.

In any case, this is not something caused or affected by Angular.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests