日本語

How to Override Response Headers in Chrome DevTools

​​Chrome DevTools​Content-Security-Policy,CORS,HTTP

Chrome DevTools' Override headers feature lets you rewrite or add response headers that a server sends, inside your own browser only. When a fetch to an API on another origin fails with a CORS error because Access-Control-Allow-Origin is missing, you can check whether adding the header to the server would fix it, without touching the server's settings. In the test environment, I added Access-Control-Allow-Origin: * to an API that caused a CORS error and tested until the data could be fetched. I include the screens and numbers from that test.

Note

The test environment was Ubuntu 24.04 and Chrome for Testing 153.0.8010.12 (the version bundled with Playwright 1.63.0). I started Chrome with a window on a virtual display and operated the DevTools docked on the right side of the window by sending mouse and keyboard input. I set DevTools to English, so the buttons and menus are written in English here. I served the page and the API from two HTTP servers I started locally (http://localhost:8080/ and http://localhost:8081/). I did not test Chrome on macOS or Windows, fetches that send a preflight (OPTIONS) request, or overrides that remove headers the server returned.

The test page and the results before overriding

The page (http://localhost:8080/) fetches stock and price JSON from an API on a different port (http://localhost:8081/) and displays them. Because the port numbers differ, the page and the API are different origins. The API server does not return Access-Control-Allow-Origin.

html
<p id="stock">在庫を確認中…</p>
<p id="price">価格を確認中…</p>
<script>
const show = async (id, url) => {
  const el = document.getElementById(id);
  try {
    const res = await fetch(url);
    const data = await res.json();
    el.textContent = data.label;
  } catch (e) {
    el.textContent = '取得できませんでした(' + e.name + ': ' + e.message + ')';
  }
};
show('stock', 'http://localhost:8081/api/stock.json');
show('price', 'http://localhost:8081/api/price.json');
</script>

The headers the API returns are as follows.

text
$ curl -sI http://localhost:8081/api/stock.json
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.11.18
Date: Sat, 21 Nov 2026 21:49:30 GMT
Content-type: application/json
Content-Length: 33
Last-Modified: Sat, 21 Nov 2026 21:33:20 GMT

When I opened the page, both requests displayed the message "Could not fetch (TypeError: Failed to fetch)". The Network panel showed "CORS error" as the status for stock.json and price.json. The API server's access log showed GET /api/stock.json and GET /api/price.json arriving, and both returned 200. The server responded, but the browser did not pass the body to the page because of the CORS rules.

Choose Override headers in the Network panel

The official documentation on overriding response headers explains how to right-click a request in the Network panel and select Override headers.

Context menu opened by right-clicking the stock.json row in the Network panel. Near the bottom are Override headers, a greyed-out Override content, and Show all overrides
Right-clicking the stock.json row, which showed a CORS error

Right-clicking the stock.json row with the CORS error showed Override headers near the bottom of the menu. Override content was greyed out and could not be selected.

I had not yet chosen a folder to store the override files. When I pressed Override headers, a bar reading "Select a folder to store override files in" with a Select folder button appeared at the top of DevTools. I went through these steps:

  1. Press Select folder, choose a folder in the dialog that opens, and press Open.
  2. When "Let DevTools edit files?" appears at the top of the screen, press Allow.

For the test, I created an empty folder at the example path /path/to/overrides and selected it. After I pressed Allow, the Headers tab for stock.json opened in the Network panel, and an Add header button appeared below the Response headers list. At this point, an empty .headers file (0 bytes) had been created in the folder.

text
overrides/
└── localhost:8081/
    └── api/
        └── .headers

At this point, the Disable cache checkbox in the Network panel was also checked. (It had been unchecked before I started overriding.) The official documentation's limitations section also says that the cache is disabled while local overrides are enabled.

Add a header with Add header, then reload

Pressing Add header adds a row at the end of Response headers where you can enter a name and a value. I entered Access-Control-Allow-Origin as the name and * as the value, then pressed Enter.

Response headers list. Below Content-Length, Content-Type, Date, Last-Modified, and Server, a new row with Access-Control-Allow-Origin and * has been added. It has a green vertical bar on the left and a trash can button on the right
After adding Access-Control-Allow-Origin: * with Add header

As soon as I pressed Enter, the following content was written to the .headers file. The header name is saved in lowercase.

json
[
  {
    "applyTo": "stock.json",
    "headers": [
      {
        "name": "access-control-allow-origin",
        "value": "*"
      }
    ]
  }
]

Adding the header alone did not change the page, which still showed "Could not fetch". The official documentation also says you need to reload the page for the change to take effect. The results after reloading are below.

Top: the page shows "In stock: 3 items" (the labels are in Japanese) and "Could not fetch (TypeError: Failed to fetch)". Bottom: the Network panel list, where the stock.json status is 200 and price.json is a CORS error
After reloading. Top: the page. Bottom: the Network panel
Request Before overriding After adding the header and reloading
stock.json CORS error, could not fetch 200, "In stock: 3 items"
price.json CORS error, could not fetch CORS error, could not fetch

I added the header only for stock.json, so price.json still failed. The API server's access log again showed GET /api/stock.json and GET /api/price.json after the reload. The requests reached the server, and only the response headers the browser received were rewritten.

You can tell which requests the override applies to from the DevTools display. In the test, the icon on the stock.json row had a purple dot, and the tooltip on hover said "Request headers are overridden." (The label says "Request," but the header I changed was a response header.) A warning triangle icon appeared next to the Network tab name. When I reopened the Headers tab for stock.json, the Access-Control-Allow-Origin row was shown with a green background. The official documentation says that rewritten headers are shown in green.

Apply a rule to many requests with the .headers file

Clicking the .headers link to the right of the Response headers heading switches to the Overrides tab of the Sources panel and opens that .headers file. The editor shows which requests the rule applies to (Apply to) and the headers to add.

I changed Apply to from stock.json to *.json, pressed Enter, and reloaded the page.

Top: the page shows "In stock: 3 items" and "Price: 1,200 yen" (the labels are in Japanese). Bottom: the Overrides tab of the Sources panel, showing overrides, localhost:8081/api, and .headers, with the editor containing Apply to: *.json and access-control-allow-origin: *
After changing Apply to to *.json and reloading. Top: the page. Bottom: the Sources panel

The applyTo value in the file changed to "*.json", and the page showed both "In stock: 3 items" and "Price: 1,200 yen." The official documentation says that * matches multiple characters and ? matches one character. With *.json, one rule applies to all .json requests in this folder (localhost:8081/api).

The official documentation says to save the .headers file with Ctrl+S (Command+S on macOS) after editing it. In the test environment, the change was written to the file as soon as I pressed Enter.

Change the value of an existing header

You can also change the value of a header the server returned, not just add new ones. In the Headers tab of stock.json in the Network panel, I clicked the Content-Type value application/json, changed it to text/plain, and pressed Enter.

A new rule with applyTo set to stock.json was added to the .headers file, separate from the *.json rule.

json

{
    "applyTo": "stock.json",
    "headers": [
      {
        "name": "content-type",
        "value": "text/plain"
      }
    ]
  }

After reloading, I ran the following code in the page's context to read the Content-Type. I read price.json the same way.

js
fetch('http://localhost:8081/api/stock.json').then(r => r.headers.get('content-type'))

The results were as follows.

Point in time stock.json price.json
Before changing the value application/json application/json
After changing the value and reloading text/plain application/json
After restoring it with the trash can button and reloading application/json application/json
Response headers for stock.json. The Access-Control-Allow-Origin row and the Content-Type row, whose value is now text/plain, are shown with green backgrounds
Response headers after changing the Content-Type value and reloading

The value that the page's JavaScript could read was also the changed value. Pressing the trash can button on the row (Remove this header override) removed the stock.json rule from .headers. After reloading, the value returned to the original application/json. The official documentation also says this button removes an added header and restores the changed value to its original state.

You can also override the response headers of the page itself

You can override the headers of the HTML response, not just the headers of fetch requests. This lets you see how the page behaves before you add a Content-Security-Policy (CSP) to the server.

In the Network panel, I right-clicked the row for localhost (the page itself), and Override headers was available there too. I added a header with the name Content-Security-Policy and the value script-src 'none' (a setting that blocks all scripts from running). This created localhost:8080/.headers in the folder, and applyTo was index.html. The page URL is http://localhost:8080/, but the file name was treated as index.html.

json
[
  {
    "applyTo": "index.html",
    "headers": [
      {
        "name": "content-security-policy",
        "value": "script-src 'none'"
      }
    ]
  }
]

After reloading, the page stayed at "Checking stock…" and "Checking price…". The inline script on the page did not run, and no request went to the API. The API server's access log showed nothing. The Console showed the following message.

text
Executing inline script violates the following Content Security Policy directive 'script-src 'none''. Either the 'unsafe-inline' keyword, a hash ('sha256-knrmR19Tg/rrJBaJL4xht1xDuEocQQQO/qqW11o+XUY='), or a nonce ('nonce-...') is required to enable inline execution. The action has been blocked.

Remove or stop overrides

To remove overrides one at a time, use the trash can button, as I did with Content-Type. When I removed the CSP row in the test, the contents of localhost:8080/.headers became []. After reloading, the stock and price values appeared again.

To temporarily stop all overrides, uncheck "Local overrides" in the Overrides tab of the Sources panel. With it unchecked and the page reloaded, both stock and price returned to "Could not fetch." The .headers files in the folder remained. When I checked the box again and reloaded, both values were displayed again.

Header overrides only take effect while DevTools is open. In the test, I closed DevTools with overrides still enabled and reloaded. The results were as follows.

Action Page display Requests that reached the API server
Closed DevTools, then F5 Both: could not fetch None
F5 again Both: could not fetch None
Ctrl+Shift+R Both: could not fetch stock.json and price.json (both 200)
Reopened DevTools with F12, then F5 "In stock: 3 items" / "Price: 1,200 yen" stock.json and price.json (both 200)
Point

If Override headers makes a CORS error go away, you can expect that the same change will work once the server returns the same header. But the override only changes responses in your own browser, while DevTools is open.

As the last row of the table shows, .headers rules remain even after you close DevTools. When you finish testing, either uncheck Local overrides or delete the rules. Otherwise, the next time you open DevTools, the page will still run with the rewritten headers.

​Posted by 86