Andres
Andres

Reputation: 3414

How to remove HTTP specific headers in Javascript

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
Authorization: Basic Og==
Host: test.myhost.com

Regards

Upvotes: 21

Views: 46954

Answers (4)

Fancyoung
Fancyoung

Reputation: 2423

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");

Upvotes: 5

Dmitry Efimenko
Dmitry Efimenko

Reputation: 11203

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.

Upvotes: -1

Adam
Adam

Reputation: 44929

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", "");

Upvotes: 15

BalusC
BalusC

Reputation: 1108642

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.

Upvotes: 3

Related Questions