Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Angular HttpClient does not add the X-XSRF-TOKEN header to a request

ErrorRequest headers contain no X-XSRF-TOKEN

Short answer

Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script. Check the request method, whether the URL points to another origin, whether the cookie and header names match your server, and whether the cookie is HttpOnly.

  • Applies to Angular 19, 20 and 21, @angular/common/http, browsers with standard cookie rules
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser console on the Angular page and check whether the cookie is visible to script. If its name is missing from the output, Angular has no value to copy.

    document.cookie
  2. 2Match what you see, then jump to its fix

    1. 1The header is missing only on read requests; POST, PUT and DELETE carry itThe request is a GET, HEAD or QUERYFix
    2. 2The page is on localhost:4200 and the API URL is an absolute URL such as http://localhost:8080/apiThe request goes to another origin (a different host or port)Fix
    3. 3The cookie exists, for example CSRF-TOKEN, but Angular never sends itThe server uses other cookie and header names than the Angular defaultsFix
    4. 4The cookie shows in the Application tab but is missing from document.cookieThe cookie cannot be read by scriptFix
    5. 5No header on any POST, and the app config calls withNoXsrfProtectionXSRF protection was switched off in the HttpClient setupFix

What does the error look like?

  • Request headers contain no X-XSRF-TOKEN
  • 403 Forbidden on POST, PUT or DELETE while GET works

Cause 1 of 5

The request is a GET, HEAD or QUERY

1Check

In the HttpClient source the interceptor skips these methods, with the note that non-mutating requests generally do not need a token.

grep -rn "http.get\|http.head" src/app | head

2Fix

Do not rely on the header for reads. If the server really needs it on a read, add it yourself in an interceptor.

export const csrfOnGet: HttpInterceptorFn = (req, next) => {
  const token = inject(HttpXsrfTokenExtractor).getToken();
  return token && req.method === 'GET'
    ? next(req.clone({ setHeaders: { 'X-XSRF-TOKEN': token } }))
    : next(req);
};

3Verify

The GET request now lists the header in the network tab.

curl -i -H "X-XSRF-TOKEN: test" https://example.com/api/items

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 5

The request goes to another origin (a different host or port)

1Check

The interceptor compares the origin of the page with the origin of the request URL and passes the request through unchanged when they differ. Print both.

console.log(location.origin);
console.log(new URL('http://localhost:8080/api/items', location.href).origin);

2Fix

Serve the API and the Angular app from one origin, for example behind one reverse proxy, and call it with a relative URL such as /api/items.

this.http.post('/api/items', body).subscribe();

3Verify

The POST now lists X-XSRF-TOKEN in its request headers.

document.cookie

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 5

The server uses other cookie and header names than the Angular defaults

1Check

Angular reads the cookie named XSRF-TOKEN and writes the header X-XSRF-TOKEN by default. Compare these names with what your server sets and expects.

curl -s -D - -o /dev/null https://example.com/ | grep -i set-cookie

2Fix

Tell HttpClient the names your server uses.

provideHttpClient(
  withXsrfConfiguration({ cookieName: 'CSRF-TOKEN', headerName: 'X-CSRF-TOKEN' })
)

3Verify

The renamed header appears on the next POST.

document.cookie

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 5

The cookie cannot be read by script

1Check

A cookie with HttpOnly is hidden from document.cookie, and Angular reads the token from there. A cookie with a Path or Domain that does not fit the page is not sent or visible either.

curl -s -D - -o /dev/null https://example.com/ | grep -i set-cookie

2Fix

Set the token cookie without HttpOnly (the session cookie can stay HttpOnly), with Path=/ and no Domain unless you need subdomains.

Set-Cookie: XSRF-TOKEN=<token>; Path=/; Secure; SameSite=Lax

3Verify

The cookie name now appears in the console output.

document.cookie

Confirmed by developer.mozilla.org

Did this fix it?

Cause 5 of 5

XSRF protection was switched off in the HttpClient setup

1Check

The feature registers XSRF_ENABLED as false for that HttpClient instance.

grep -rn "withNoXsrfProtection" src/

2Fix

Remove withNoXsrfProtection from provideHttpClient, or from the feature list that includes it.

provideHttpClient(withInterceptors([authInterceptor]))

3Verify

Rebuild and send a POST; the header is present.

grep -rn "withNoXsrfProtection" src/ | wc -l

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Serve the app and its API from one origin so relative URLs work in development and in production.
  • Agree one cookie name and one header name between server and Angular, and set them in one place.
  • Add an end-to-end test that posts a form and checks the header.
  • Never copy the token into an absolute URL to another host; send it only to origins you control.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.