Bird
0
0

Which of the following is the correct way to clone an HTTP request before adding an authentication token in an Angular interceptor?

easy📝 Syntax Q12 of 15
Angular - HTTP Client
Which of the following is the correct way to clone an HTTP request before adding an authentication token in an Angular interceptor?
Arequest.updateHeaders({ Authorization: 'Bearer ' + token })
Brequest.headers.set('Authorization', 'Bearer ' + token)
Crequest.modifyHeaders('Authorization', 'Bearer ' + token)
Drequest.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) })
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular HttpRequest immutability

    HttpRequest objects are immutable, so you must clone them to modify headers.
  2. Step 2: Use the clone method with headers.set()

    The correct syntax is to call clone() and set the new header inside it.
  3. Final Answer:

    request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) }) -> Option D
  4. Quick Check:

    Clone request with headers.set() = A [OK]
Quick Trick: Always clone requests before changing headers [OK]
Common Mistakes:
MISTAKES
  • Trying to modify headers directly without cloning
  • Using non-existent methods like modifyHeaders or updateHeaders
  • Not using the clone method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes