Bird
0
0

Which of the following is the correct way to create HttpHeaders with a 'Content-Type' of 'application/json'?

easy📝 Syntax Q3 of 15
Angular - HTTP Client
Which of the following is the correct way to create HttpHeaders with a 'Content-Type' of 'application/json'?
Anew HttpHeaders.set('Content-Type', 'application/json')
Bnew HttpHeaders({'Content-Type': 'application/json'})
Cnew HttpHeaders.append('Content-Type', 'application/json')
Dnew HttpHeaders().add('Content-Type', 'application/json')
Step-by-Step Solution
Solution:
  1. Step 1: Check HttpHeaders constructor usage

    The constructor accepts an object with header keys and values, so new HttpHeaders({'Content-Type': 'application/json'}) is valid.
  2. Step 2: Understand method chaining

    set() returns a new instance, so chaining without assignment won't work. append() exists but is used differently. add() is invalid.
  3. Final Answer:

    new HttpHeaders({'Content-Type': 'application/json'}) -> Option B
  4. Quick Check:

    Headers creation = constructor object [OK]
Quick Trick: Use constructor object to set headers initially [OK]
Common Mistakes:
MISTAKES
  • Using add() which does not exist
  • Chaining set() without assignment
  • Using append() incorrectly for single header

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes