Tarun Gulati
Tarun Gulati

Reputation: 31

Chrome WebAPIchrome.webRequest.onBeforeSendHeaders not changing the params in the outgoing requests

I am trying to add and IP to the X-Forwarded-For param in the requestHeader, but my code as per the examples on Chrome API isn't doing so.

I have the following code:

var requestFilter = {
    urls: [ "<all_urls>" ]
  },

  extraInfoSpec = ['requestHeaders', 'blocking'],

  handler = function( details ) {

var headers = details.requestHeaders,
  blockingResponse = {};

  var isXForwardedForSet = false;

for (var i = 0, l = headers.length; i < l; ++i) {
  if (headers[i].name === 'X-Forwarded-For') {
      headers[i].value = "42.104.0.0";
      isXForwardedForSet = true;
      break;
  }
}

if (!isXForwardedForSet) {
    headers.push({
        name: "X-Forwarded-For",
        value: "42.104.0.0"
    });
}

blockingResponse.requestHeaders = headers;

   return blockingResponse;

  };

chrome.webRequest.onBeforeSendHeaders.addListener( handler, requestFilter,      extraInfoSpec );

Upvotes: 1

Views: 239

Answers (1)

Xan
Xan

Reputation: 77531

Proxy-related headers are probably ignored.

Quoting the docs:

Note that the web request API presents an abstraction of the network stack to the extension. Internally, one URL request can be split into several HTTP requests (for example to fetch individual byte ranges from a large file) or can be handled by the network stack without communicating with the network. For this reason, the API does not provide the final HTTP headers that are sent to the network. For example, all headers that are related to caching are invisible to the extension.

There is no comprehensive list of headers that are ignored, but some are listed in the docs. Common-sense criterion: if it represents knowledge about the network (caching, proxy, etc.), the API cannot touch it.

Upvotes: 1

Related Questions