Add Access-Control-Allow-Origin for local testing
When your front end on http://localhost:3000 calls an API on another origin that doesn't send CORS headers, the browser blocks the response. For development you can add the missing headers in the browser instead of changing the server. In HeaderDial, add a response header Access-Control-Allow-Origin: *, and Access-Control-Allow-Headers: * if you send custom headers, scoped with a domain filter to the API host. Fix the server properly before you ship.
Access-Control-Allow-Origin: * to every site, any page you visit can read responses from those hosts with your browser, including services on your local network. Always scope this profile to the API you are working on and switch it off afterwards.Simple and preflighted requests
Browsers treat cross-origin requests in two ways:
| Simple request | Preflighted request | |
|---|---|---|
| When | GET, HEAD or POST, only CORS-safelisted headers (such as Accept, Accept-Language, Content-Language), and a Content-Type of text/plain, multipart/form-data or application/x-www-form-urlencoded | Anything else: PUT, DELETE, PATCH, a JSON Content-Type, an Authorization header, any custom header |
| What happens | The request is sent; the browser only lets your code read the response if it carries a matching Access-Control-Allow-Origin | The browser first sends an OPTIONS request with Access-Control-Request-Method and Access-Control-Request-Headers. The real request is only sent if that preflight response allows it |
The response headers you add in HeaderDial apply to both the preflight response and the real response, as long as they match your filter. We verified in an automated Chromium test that with Access-Control-Allow-Origin: * and Access-Control-Allow-Headers: * added, a simple cross-origin GET and a preflighted request with a custom header both succeed. In that test the server answered OPTIONS with an empty 204 and no CORS headers at all.
The server still has to answer the OPTIONS request with a success status. If it returns 404, 405 or a redirect for OPTIONS, the preflight fails no matter what headers are added. For methods other than GET, HEAD and POST, also add Access-Control-Allow-Methods (for example GET, POST, PUT, PATCH, DELETE), and test your actual calls.
To look up the exact console error you see, or to compare this with starting Chrome with --disable-web-security, see Fix a “blocked by CORS policy” error while testing.
Steps
- Click the HeaderDial icon. Grant site access when the banner asks; Only <current host> is not enough here if the API is on a different host from the page. The browser only changes a response when the extension has access to both the page's host and the API's host, so also grant the API host (open an API URL in a tab and choose Only <that host> there) or choose Allow on all sites. Our automated Chromium test checks all three cases: page host only and API host only both leave the call blocked, and both hosts together make it succeed.
- Click + next to the profile chips to create a profile and name it, for example, "CORS (dev)". A separate profile lets you turn it on and off with its Active switch.
- Click + Response header. Name
Access-Control-Allow-Origin, value*, operation set (this replaces a value the server already sends). - If your requests are preflighted, add
Access-Control-Allow-Headerswith value*, andAccess-Control-Allow-Methodsif you use methods such asPUTorDELETE. - Open Where it applies, click + Only on…, choose Domain and enter the API host, such as
localhostorapi.dev.example.com. Filters match the URL of the request, so this targets calls to the API, whichever page makes them. - Optionally, tick only the
xmlhttprequestrequest type, which coversfetch()and XHR calls, so pages and images are left alone. - Reload your app. The CORS error in the Console should be gone.
Credentials need an exact origin
If your code sends cookies or HTTP auth (fetch(url, { credentials: 'include' }) or xhr.withCredentials = true), the wildcard is not allowed. The browser rejects * for credentialed requests. Set instead:
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Credentials: true
Use your front end's exact origin, including scheme and port. In the same situation Access-Control-Allow-Headers and Access-Control-Allow-Methods must list names explicitly rather than use *.
Check what the server sends with curl
CORS is enforced by browsers, not by curl, but curl shows you what the server answers. That tells you which headers are missing:
# the preflight
curl -i -X OPTIONS http://localhost:8080/api/items \
-H "Origin: http://localhost:3000" \
-H "Access-Control-Request-Method: PUT" \
-H "Access-Control-Request-Headers: content-type"
# the real request
curl -i http://localhost:8080/api/items -H "Origin: http://localhost:3000"
Pitfalls
- Reading custom response headers. Even when the request succeeds, your code can only read non-safelisted response headers listed in
Access-Control-Expose-Headers. - The error was something else. A network error, a mixed-content block (HTTPS page calling HTTP) or a server 500 can look like CORS in the console. Check the Network panel for the real status.
- Forgetting it is on. Give the rows an expiry such as 8 hours, or watch the toolbar badge, which counts active headers.
- Fix the server. Before release, configure the API to send the right CORS headers for the origins that need them. Don't rely on the browser.
Add to ChromeFirefox: coming soon