I'm using jQuery.ajax to make HTTP connections. In my project I'd like to set custom http headers, but I'd like to send only the ones I have set. Currently my headers are being sent, but in addition there are also a number of other headers I didn't set.
This is how I'm adding custom headers:
$.ajax("http://fakedomain.foo", {headers: {header1: "value1", header2: "value2"}});
I might be fine even in case it's not possible to set the whole collection of headers, but only add extra ones. But even in this case, (削除) sometimes my headers are modified before being sent. For instance, if I add these headers (削除ここまで):
{
"Connection": "close",
"Accept-Encoding": "deflate, gzip",
"Accept": "text/plain",
}
(削除) Then this is what is actually being sent (I made a capture with Wireshark only to verify this) (削除ここまで)
{
"Connection": "close",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept": "text/plain, */*; q=0.01",
"Accept-Language": "es-ES,es;q=0.8",
"Dnt": "1",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
...
}
(削除) As you can see some evil gobling out there is tweaking my headers, and it has added the "sdch" encoding to the "Accept-Encoding" header, and also changed the "Accept" header so that it also accepts */*. This is a real problem if the WS is picky and needs to take different actions based on the headers. (削除ここまで)
So to sum up:
Is there a way to send only my headers and no extra ones?
(削除) 2. If not, is there a way to add my custom headers as they are, without them being modified? (削除ここまで)
UPDATE:
My bad, I was not setting the headers properly. (The code I posted is correct, but there was a bug in my unit tests, so my headers were not actually added and the default ones were sent instead. After fixing this, I can see my headers are being sent with the correct text, and no modifications.
So forget about #2. The question #1 still applies.
-
11) No. 2) No. The headers which get sent are dependant on the broswer, although they will all modify them in some way.Rory McCrossan– Rory McCrossan2013年10月03日 10:58:27 +00:00Commented Oct 3, 2013 at 10:58
1 Answer 1
The specification (W3C, WHATWG) forbids overriding some headers, including Accept-Encoding.
Interestingly, the spec does not say anything special about Accept, which means the browser must use it as given. In this case it's jQuery at fault, where it adds that last part just in case.
It was there since 6 years ago.
The specification also mandates the browser to send some other headers, such as Authorization, Host, Connection, Keep-Alive, and others.
8 Comments
Explore related questions
See similar questions with these tags.