Bird
0
0

Identify the error in this Angular HTTP request code:

medium📝 Debug Q14 of 15
Angular - HTTP Client
Identify the error in this Angular HTTP request code:
const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json');
this.http.post('/api/data', body, { headers }).subscribe();
AHeaders must be plain object, not HttpHeaders instance
BMissing import for HttpHeaders
CHttpHeaders is immutable; set() does not change original headers
Dpost() method requires params object
Step-by-Step Solution
Solution:
  1. Step 1: Understand HttpHeaders immutability

    HttpHeaders methods like set return new instances; original is unchanged.
  2. Step 2: Analyze code behavior

    Headers remain empty because set result is not assigned back.
  3. Final Answer:

    HttpHeaders is immutable; set() does not change original headers -> Option C
  4. Quick Check:

    HttpHeaders immutable = assign set() result [OK]
Quick Trick: Assign set() result to headers variable [OK]
Common Mistakes:
MISTAKES
  • Not assigning set() result back
  • Assuming headers object changes in place
  • Confusing HttpHeaders with plain objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes