NG.VII · Angular 22 practice question
A developer wants an interceptor to attach an X-Trace-Id header to every outgoing request. Which snippet correctly creates the updated request?
const reqWithHeader = req.clone({
headers: req.headers.set('X-Trace-Id', 'abc-123'),
});
Check answer and explanation
Correct answer: The snippet is valid because req.clone() pairs with the immutable req.headers.set() update.
Explanation
The snippet is valid because req.clone() pairs with the immutable req.headers.set() update to produce a new HttpRequest with the added header.
Common trap
HttpHeaders is immutable, so req.headers.set() returns a new HttpHeaders instance rather than mutating in place and returning void. req.clone() accepts a configuration object that supports updating headers, params, and other request properties.
Source: Angular docs: Interceptors — Modifying requests
Checked against the cited text on 2026-11-25.