Bird
0
0

What is wrong with this interceptor code snippet?

medium📝 Debug Q7 of 15
Angular - HTTP Client
What is wrong with this interceptor code snippet?
intercept(req, next) {
  const modifiedReq = req.clone();
  modifiedReq.headers.set('X-Custom', 'value');
  return next.handle(modifiedReq);
}
Anext.handle() must be called with original req
Bclone() must include headers explicitly to modify
CHeaders are immutable; set() does not change headers on cloned request
DInterceptor must return a Promise, not Observable
Step-by-Step Solution
Solution:
  1. Step 1: Analyze header modification after cloning

    Even after cloning, HttpHeaders are immutable; calling set() returns a new headers object but does not update the cloned request's headers.
  2. Step 2: Correct way to modify headers

    Headers must be set inside clone() options to update the request headers.
  3. Final Answer:

    Headers are immutable; set() does not change headers on cloned request -> Option C
  4. Quick Check:

    Modify headers inside clone() options [OK]
Quick Trick: Set headers inside clone() to update them [OK]
Common Mistakes:
MISTAKES
  • Calling set() on headers after cloning without reassigning
  • Expecting headers to change without clone options
  • Returning Promise instead of Observable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes