Guide
How to add CORS headers in Chrome for local development
Chrome enforces CORS using the response headers the server sends. While the backend’s CORS
config isn’t ready, you can add Access-Control-Allow-Origin and related headers
to one API’s responses, so your app on localhost can read them. Keep it to that API and turn
it off when you are done.
Add CORS headers in Headerbench
- Create a profile named Local CORS and tag it Local in Profile settings.
- Open Response headers and click the Allow CORS preset. It adds four rows: Access-Control-Allow-Origin, -Methods, -Headers and -Credentials.
- Change
Access-Control-Allow-Originfrom*to your app’s exact origin, for examplehttp://localhost:3000, scheme and port included. Browsers refuse*when the request sends cookies. - If your app sends
Authorizationor JSON, changeAccess-Control-Allow-HeaderstoAuthorization, Content-Type.*does not cover Authorization. - Open Filters and add an include filter of type Domain with
api.example.com. - Open your app, turn the profile on in the popup and allow access to
api.example.comand to your app’s page when Chrome asks. - Reload. The popup’s Applied on this tab lists the response rows against each API call.
Profile Local CORS (env: Local)
Response headers
Set Access-Control-Allow-Origin http://localhost:3000
Set Access-Control-Allow-Methods GET, POST, PUT, PATCH, DELETE, OPTIONS
Set Access-Control-Allow-Headers Authorization, Content-Type
Set Access-Control-Allow-Credentials true
Filters
Include Domain api.example.com
Development only. A profile that adds these headers to every site lets any page you open read responses from those sites with your cookies, which is what CORS exists to prevent. Keep the domain filter, and in the popup’s menu for the profile choose This tab only or Pause after 1 hour. The real fix belongs in the server’s CORS config.
Which header fixes which console error
-
No 'Access-Control-Allow-Origin' header is present on the requested resource: set
Access-Control-Allow-Origin. -
The value of the 'Access-Control-Allow-Origin' header in the response must not be the
wildcard '*' when the request's credentials mode is 'include': use the exact origin and keep
Access-Control-Allow-Credentials: true. -
Request header field authorization is not allowed by Access-Control-Allow-Headers in
preflight response: list the header in
Access-Control-Allow-Headers. -
Method PATCH is not allowed by Access-Control-Allow-Methods: add the method to
Access-Control-Allow-Methods. -
Your code cannot read a custom response header such as
X-Total-Count: add a rowAccess-Control-Expose-Headers: X-Total-Count.
Preflight requests
Before a PUT, PATCH or DELETE, a request with an Authorization header, or a JSON POST, the
browser sends an OPTIONS preflight and checks its response first. On
Filters, keep Methods on All methods, or include
OPTIONS, so the rule covers it.
A header rule cannot change a status code. If your server answers the preflight with 401,
404 or 405, the browser rejects it whatever the headers say, and the console reports that
the preflight does not have HTTP ok status. Fix that on your side: answer OPTIONS
with 204 on the dev server, or proxy the API through your dev server (Vite’s
server.proxy, webpack’s devServer.proxy) so the call is
same-origin and CORS does not apply at all.
Check that it worked
- The CORS error is gone from the Console and your code receives the response body.
- Applied on this tab lists the four rows against the API calls.
- Test a URL with the API URL, request type Fetch / XHR and Sent from page set to your app’s URL shows every row that applies.
Chrome DevTools does not always show headers changed by an extension. If the Network panel shows the old value, trust the server’s echo or the popup’s Applied on this tab, which asks Chrome what it actually changed.
Troubleshooting
Nothing changes
Chrome applies header and redirect rules only when Headerbench has access to the request’s site
and to the page that sends the request, its initiator. A call to
api.example.com from an app on localhost:3000 needs both. Without the
page, Chrome skips the rule without an error. The popup shows a Grant button
when the current page is missing, and Test a URL has a
Sent from page field that checks the pair.
The origin is almost right
http://localhost:3000 and http://127.0.0.1:3000 are different
origins, and so are ports 3000 and 5173. Copy the origin from the Console error.
The server sends its own CORS headers
Set replaces the server’s value, so a wrong origin from the server is overwritten. If two profiles that are on set the same header, the higher one in the popup list wins.
Without an extension
- A dev-server proxy, as above, is the cleanest fix and needs nothing in the browser.
- DevTools can override response headers: in the Network panel, right-click the request, choose Override headers and add the header. It needs a local overrides folder and applies while DevTools is open.
-
Starting Chrome with
--disable-web-security --user-data-dir=/tmp/chrome-devturns off the same-origin policy for that whole browser instance. Use the separate profile directory, never sign in or browse normally in it, and close it when you are done.