Bird
0
0

Why does this interceptor fail to add the token?

medium📝 Debug Q7 of 15
Angular - HTTP Client
Why does this interceptor fail to add the token? ```typescript intercept(req, next) { const token = this.authService.getToken(); const cloned = req.clone(); cloned.headers.set('Authorization', `Bearer ${token}`); return next.handle(cloned); } ```
AHeaders are immutable; set() does not modify cloned.headers in place
BgetToken() returns a Promise and needs await
Cclone() must include headers explicitly
Dnext.handle() must be called with original req
Step-by-Step Solution
Solution:
  1. Step 1: Understand immutability of HttpHeaders

    set() returns a new HttpHeaders instance; it does not modify in place.
  2. Step 2: Identify missing assignment of new headers

    Must assign the result of set() back to headers when cloning.
  3. Final Answer:

    Headers are immutable; set() does not modify cloned.headers in place -> Option A
  4. Quick Check:

    HttpHeaders set() returns new instance [OK]
Quick Trick: Assign set() result to headers when cloning request [OK]
Common Mistakes:
MISTAKES
  • Assuming set() modifies headers directly
  • Not assigning new headers to cloned request
  • Ignoring async token retrieval

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes