NG.X · Angular 22 practice question
An interceptor needs to attach an X-Authentication-Token header to every outgoing HTTP request before forwarding it. How should it modify the request?
const clonedRequest = request.clone({
headers: request.headers.append('X-Authentication-Token', authService.getAuthToken()),
});
return next(clonedRequest);
Check answer and explanation
Correct answer: Call request.clone() with request.headers.append(...) and pass the cloned request to next().
Explanation
HttpRequest and HttpHeaders are immutable, so the interceptor clones the request with the appended header and forwards the cloned request to next().
Common trap
This is tempting because mutating objects is common in JavaScript, but HttpRequest and HttpHeaders are immutable so in-place mutation does not attach the header. This is tempting because interceptors return Observable<HttpEvent<unknown>>, but skipping next() would prevent the request from continuing down the handler chain.
Source: Angular docs: Test requests — Testing an Interceptor
Checked against the cited text on 2026-11-25.