开发者

How to remove HTTP specific headers in Javascript

开发者 https://www.devze.com 2022-12-23 10:53 出处:网络
Is it possible to, before sending a http message, remove some specific http headers using javascript / XmlHttpRequest ?

Is it possible to, before sending a http message, remove some specific http headers using javascript / XmlHttpRequest ?

I'm using a proprietary browser, so there's no way to do it using browser specific solution.

For example, I want to remove the header 'Authorization' before send the message

POST /social/rpc?oauth_version=1.0& ... HTTP/1.1

Accept: text/html, image/png, image/*, */*
Accept-Language: ko
Author开发者_开发百科ization: Basic Og==
Host: test.myhost.com

Regards


You could use the setRequestHeader method of the XmlHttpRequest object assuming your browser supports it, It is part of the W3C spec. It is also implemented by IE.

var req = new XMLHttpRequest();
req.setRequestHeader("Authorization", "");


When I use jquery-file-upload, and want to remove the header in the options method, setting it to null or '' doesn't work for me. I use this instead:

req.setRequestHeader("Authorization");


Never done it, but in theory you could try:

xhr.setRequestHeader('Authorization', null);

There's also an unspecified removeRequestHeader() function in some implementations, you may want to give it a try as well.


Adam's answer didn't work for me. However, the following did:

xhr.setRequestHeader('Authorization', ' ');

notice, second parameter is a string containing a space instead of empty space. It does not remove header completely, but sets it to the empty string, which might be enough for some cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消