Angular HttpClient does not add the X-XSRF-TOKEN header to a request
Request headers contain no X-XSRF-TOKENShort 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.
Fix it fast
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.cookie2Match what you see, then jump to its fix
- 1
The header is missing only on read requests; POST, PUT and DELETE carry itThe request is a GET, HEAD or QUERYFix - 2
The 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
The cookie exists, for example CSRF-TOKEN, but Angular never sends itThe server uses other cookie and header names than the Angular defaultsFix - 4
The cookie shows in the Application tab but is missing from document.cookieThe cookie cannot be read by scriptFix - 5
No header on any POST, and the app config calls withNoXsrfProtectionXSRF protection was switched off in the HttpClient setupFix
- 1
What does the error look like?
Request headers contain no X-XSRF-TOKEN403 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 | head2Fix
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/itemsConfirmed by raw.githubusercontent.com
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.cookieConfirmed by raw.githubusercontent.com
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-cookie2Fix
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.cookieConfirmed by raw.githubusercontent.com
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-cookie2Fix
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=Lax3Verify
The cookie name now appears in the console output.
document.cookieConfirmed by developer.mozilla.org
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 -lConfirmed by raw.githubusercontent.com
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
Security
POST requests return 403 in Spring Security because the CSRF token is missing
MissingCsrfTokenException
Spring Security checks a CSRF token on every request except GET, HEAD, TRACE and OPTIONS.
4 causes, each with a fixSecurity
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fix