HeaderDial › Guides

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.

Development only. CORS protects users. If you add 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 requestPreflighted request
WhenGET, 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-urlencodedAnything else: PUT, DELETE, PATCH, a JSON Content-Type, an Authorization header, any custom header
What happensThe request is sent; the browser only lets your code read the response if it carries a matching Access-Control-Allow-OriginThe 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

  1. 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.
  2. 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.
  3. Click + Response header. Name Access-Control-Allow-Origin, value *, operation set (this replaces a value the server already sends).
  4. If your requests are preflighted, add Access-Control-Allow-Headers with value *, and Access-Control-Allow-Methods if you use methods such as PUT or DELETE.
  5. Open Where it applies, click + Only on…, choose Domain and enter the API host, such as localhost or api.dev.example.com. Filters match the URL of the request, so this targets calls to the API, whichever page makes them.
  6. Optionally, tick only the xmlhttprequest request type, which covers fetch() and XHR calls, so pages and images are left alone.
  7. 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

Add to ChromeFirefox: coming soon