HeaderDial › Guides

How to add an Authorization header to GraphQL requests in the browser

To send a token on a web app's GraphQL calls, add a request header named Authorization with the value Bearer <token> in HeaderDial, limit it to the API host with a Domain filter, and tick only the xmlhttprequest request type. The token then goes on the fetch() POSTs to /graphql but not on page loads. In Chrome 159 and Firefox 158, a header added this way did not appear in the CORS preflight's Access-Control-Request-Headers. So the cross-origin call worked even though the server allowed only content-type. A page that sends Authorization itself needs the server to allow it.

How a GraphQL call reaches the server

Most GraphQL clients send each query as a fetch() POST with a JSON body:

POST /graphql
Content-Type: application/json
Authorization: Bearer eyJhbGciOi...

{"query":"{ viewer { name } }"}

When the API is on another origin (say the app is on app.example.com and the API on api.example.com), the JSON content type makes the call "preflighted". The browser first sends an OPTIONS request listing the headers the page wants to send in Access-Control-Request-Headers, and only sends the POST if the server's Access-Control-Allow-Headers answer allows them. The CORS guide explains simple and preflighted requests in more detail.

What we measured

We checked this with HeaderDial 0.3.0 in Chrome 159.0.8154.6 and Firefox 158.0.1 in November 2026. A page on http://127.0.0.1:port sent GraphQL queries to http://localhost:port/graphql, a different origin. The server answered preflights with Access-Control-Allow-Headers: content-type only, and logged every request. The HeaderDial rule set Authorization: Bearer test-token, limited to the domain localhost. Both browsers behaved identically. Edge uses Chrome's engine, but we did not test it separately.

RequestNo ruleRule, no request types tickedRule, xmlhttprequest onlyRule, POST only
Preflight OPTIONSno tokentoken senttoken sentno token
Access-Control-Request-Headers on the preflightcontent-typecontent-typecontent-typecontent-type
fetch() POST with JSONno token; viewer was nulltoken sent; data returnedtoken sent; data returnedtoken sent; data returned
fetch() POST as text/plain (no preflight)no tokentoken sent, still no preflighttoken sent, still no preflightnot tested
Opening /graphql in a tab (a GraphiQL-style page)no tokentoken sentno tokenno token
WebSocket handshake to /graphqlno tokentoken sentno token (tick websocket too: sent)not tested

Two things follow. A header that HeaderDial adds is not something the page asked for, so it is not declared in the preflight, and the server does not have to allow it. The preflight itself also counts as an xmlhttprequest request, so it carries the token unless you limit the rule to POST.

We also checked the case where the page sends its own Authorization:

Page sends Authorization: Bearer from-pageResult in both browsers
Server allows only content-type, no ruleblocked; the preflight listed authorization,content-type
Server allows only content-type, HeaderDial rule onstill blocked; the rule does not change what the preflight declares
Server allows content-type, authorization, HeaderDial rule onallowed; the server received HeaderDial's token, not the page's

Steps

  1. Click the HeaderDial icon. In the site-access banner, choose Allow on all sites. If you allow single hosts instead, allow both the app's host and the API host.
  2. Click + Request header, type Authorization (it is in the suggestions) and enter Bearer, a space and your token. Keep the operation on set; it replaces any token the app sends itself. If the token is short-lived, choose an expiry such as 1 hour.
  3. Open Where it applies, click + Only on…, choose Domain and enter the API host, for example api.example.com.
  4. Under Request types (none ticked = all, including page loads), tick xmlhttprequest. That covers fetch() and XHR. If subscriptions run over a WebSocket and your server reads the token from the handshake headers, tick websocket too.
  5. Optionally, under Methods, tick POST (and GET if your client sends persisted queries as GET). Preflights then go out without the token.
  6. Run a query that needs authentication, such as { viewer { name } } or { me { id } }, and check that it returns data instead of an authentication error.

Subscriptions over WebSocket

A header rule can only change the WebSocket handshake, the GET request with Upgrade: websocket. Many subscription clients don't send the token there. The graphql-ws protocol, for example, lets the client send it in the payload of its first message, connection_init. HeaderDial cannot change message contents. Check which of the two your server reads before relying on the websocket type.

The curl equivalent

curl -s https://api.example.com/graphql \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  --data '{"query":"{ viewer { name } }"}'

curl sends no preflight, so a request that works in curl can still fail in the browser on CORS. Compare the two to tell an authentication problem from a CORS problem.

Pitfalls

Always add a Domain filter. Without one, the token goes to every host the browser talks to, including analytics and error-reporting services that the app also calls with fetch().

Add to ChromeFirefox: coming soon