Can I modify outgoing request headers with a Chrome Extension?

Google ChromeGoogle Chrome-Extension

Google Chrome Problem Overview


I can't see an answer to this in the Developer's Guide, though maybe I'm not looking in the right place.

I want to intercept HTTP requests with a Chrome Extension, and then forward it on, potentially with new/different HTTP headers - how can I do that?

Google Chrome Solutions


Solution 1 - Google Chrome

PS: I am the author of this extension so you can blame me for anything you don't like :)

It was certainly not possible when OP asked the question but soon later Chrome released experimental WebRequest API. But now they have been included officially in Chrome Extension. You can use it modify request and response headers in Chrome.

Look at this example:

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    for (var i = 0; i < details.requestHeaders.length; ++i) {
      if (details.requestHeaders[i].name === 'User-Agent') {
        details.requestHeaders.splice(i, 1);
        break;
      }
    }
    return { requestHeaders: details.requestHeaders };
  },
  {urls: ['<all_urls>']},
  ['blocking', 'requestHeaders' /* , 'extraHeaders' */]
  // uncomment 'extraHeaders' above in case of special headers since Chrome 72
  // see https://developer.chrome.com/extensions/webRequest#life_cycle_footnote
);

If you want to use Chrome Extension, you can use Requestly which allows you to modify request and response headers as you wish. Have a look at this snapshot:

>Headers Rule

Solution 2 - Google Chrome

Modifying request headers ( https://developer.chrome.com/extensions/webRequest ) is supported in chrome 17.

Solution 3 - Google Chrome

You are looking at the right place, but intercepting HTTP requests does not exist yet, but the extension team is aware that it's a popular request and would like to get to it sometime in the near future.

Solution 4 - Google Chrome

Keep in mind that starting from chrome 72, some headers are not allowed unless you add extraHeaders in opt_extraInfoSpec So the above example in @sachinjain024's answer will look something like this:

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    for (var i = 0; i < details.requestHeaders.length; ++i) {
      if (details.requestHeaders[i].name === 'User-Agent') {
        details.requestHeaders.splice(i, 1);
        break;
      }
    }
    return { requestHeaders: details.requestHeaders };
  },
  {urls: ['<all_urls>']},
  [ 'blocking', 'requestHeaders', 'extraHeaders']
);

For more info, check the documentation Screenshot from the documentation https://developer.chrome.com/extensions/webRequest#life_cycle_footnote

Solution 5 - Google Chrome

You could install ModHeader extension and add headers:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter BoughtonView Question on Stackoverflow
Solution 1 - Google ChromeSachinView Answer on Stackoverflow
Solution 2 - Google ChromeSatyanarayana ManyamView Answer on Stackoverflow
Solution 3 - Google ChromeMohamed MansourView Answer on Stackoverflow
Solution 4 - Google ChromeMahmoud FelfelView Answer on Stackoverflow
Solution 5 - Google ChromeTaras MelnykView Answer on Stackoverflow