Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I modify outgoing request headers with a Chrome Extension?

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?

like image 607
Peter Boughton Avatar asked Jul 18 '10 02:07

Peter Boughton


People also ask

How do I edit HTTP headers in Chrome?

Modify Headeropen Chrome developers toolbar Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux). Press Command+Shift+P (Mac) or Control+Shift+P (Windows, Linux, Chrome OS) to open the Command Menu. In the User agent section disable the Select automatically checkbox.

Can HTTP headers be modified?

You can manipulate the headers of incoming HTTP requests through HTTP Request Header Modification Rules. Through these rules you can: Set the value of an HTTP request header to a literal string value, overwriting its previous value or adding a new header to the request.


1 Answers

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

like image 128
Sachin Avatar answered Oct 09 '22 06:10

Sachin