Bird
0
0

Which of the following is the correct way to add a header named Authorization with value Bearer token123 using Angular's HttpHeaders?

easy📝 Syntax Q12 of 15
Angular - HTTP Client
Which of the following is the correct way to add a header named Authorization with value Bearer token123 using Angular's HttpHeaders?
Anew HttpHeaders().set('Authorization', 'Bearer token123')
Bnew HttpHeaders('Authorization', 'Bearer token123')
CHttpHeaders.append('Authorization', 'Bearer token123')
DHttpHeaders.add('Authorization', 'Bearer token123')
Step-by-Step Solution
Solution:
  1. Step 1: Recall HttpHeaders usage

    HttpHeaders is immutable; use set method to add headers.
  2. Step 2: Check each option

    Only new HttpHeaders().set('Authorization', 'Bearer token123') uses new HttpHeaders().set() correctly; others are invalid syntax or methods.
  3. Final Answer:

    new HttpHeaders().set('Authorization', 'Bearer token123') -> Option A
  4. Quick Check:

    Use set() on new HttpHeaders() = B [OK]
Quick Trick: Use new HttpHeaders().set() to add headers [OK]
Common Mistakes:
MISTAKES
  • Trying to pass headers directly to constructor
  • Using non-existent methods like append or add
  • Not creating a new HttpHeaders instance first

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes