What happens if you try to set headers or params directly as plain objects in Angular HTTP requests?
Angular accepts plain objects for headers and params, automatically converting them to HttpHeaders and HttpParams internally. However, using the dedicated classes is recommended for immutability, chainability, and advanced features.
Click to reveal answer
advanced
Why is immutability important when working with HttpHeaders and HttpParams in Angular?
HttpHeaders and HttpParams are immutable, so methods like set() return new instances. You must assign the result to keep changes.
Click to reveal answer
Which Angular class is used to add query parameters to an HTTP request?
AHttpHeaders
BHttpParams
CHttpClient
DHttpRequest
✗ Incorrect
HttpParams is designed to hold query parameters for HTTP requests.
How do you add a header named 'Content-Type' with value 'application/json' in Angular?
Headers and params must be passed inside an options object with keys 'headers' and 'params'.
Why should you use HttpHeaders and HttpParams instead of plain objects?
APlain objects automatically convert to headers and params
BPlain objects are faster but less secure
CBecause Angular expects these classes for proper request formatting
DHttpHeaders and HttpParams are deprecated
✗ Incorrect
While plain objects are accepted and converted, using HttpHeaders and HttpParams instances provides immutability, chainability, and proper handling for complex cases.
Explain how to set custom headers and query parameters in an Angular HTTP GET request.
Think about how Angular expects headers and params as special objects.
You got /4 concepts.
Describe why immutability matters when working with HttpHeaders and HttpParams in Angular.
Consider what happens if you forget to assign the result of set().
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of setting headers in an Angular HTTP request?
easy
A. To change the URL of the request
B. To add extra information like authentication tokens or content type
C. To modify the HTTP method (GET, POST, etc.)
D. To handle the response data format
Solution
Step 1: Understand what headers do
Headers carry extra info like tokens or content type in HTTP requests.
Step 2: Compare options
Only To add extra information like authentication tokens or content type correctly describes headers' purpose; others describe different concepts.
Final Answer:
To add extra information like authentication tokens or content type -> Option B
Quick Check:
Headers add info = A [OK]
Hint: Headers carry extra info like tokens, not URL or method [OK]
Common Mistakes:
Confusing headers with URL parameters
Thinking headers change HTTP method
Assuming headers handle response data
2. Which of the following is the correct way to add a header named Authorization with value Bearer token123 using Angular's HttpHeaders?
easy
A. new HttpHeaders().set('Authorization', 'Bearer token123')
B. new HttpHeaders('Authorization', 'Bearer token123')
C. HttpHeaders.append('Authorization', 'Bearer token123')
D. HttpHeaders.add('Authorization', 'Bearer token123')
Solution
Step 1: Recall HttpHeaders usage
HttpHeaders is immutable; use set method to add headers.
Step 2: Check each option
Only new HttpHeaders().set('Authorization', 'Bearer token123') uses new HttpHeaders().set() correctly; others are invalid syntax or methods.
Final Answer:
new HttpHeaders().set('Authorization', 'Bearer token123') -> Option A
Quick Check:
Use set() on new HttpHeaders() = B [OK]
Hint: Use new HttpHeaders().set() to add headers [OK]
Common Mistakes:
Trying to pass headers directly to constructor
Using non-existent methods like append or add
Not creating a new HttpHeaders instance first
3. Given the code below, what will be the final URL requested?
A. Headers must be plain object, not HttpHeaders instance
B. Missing import for HttpHeaders
C. HttpHeaders is immutable; set() does not change original headers
D. post() method requires params object
Solution
Step 1: Understand HttpHeaders immutability
HttpHeaders methods like set return new instances; original is unchanged.
Step 2: Analyze code behavior
Headers remain empty because set result is not assigned back.
Final Answer:
HttpHeaders is immutable; set() does not change original headers -> Option C
Quick Check:
HttpHeaders immutable = assign set() result [OK]
Hint: Assign set() result to headers variable [OK]
Common Mistakes:
Not assigning set() result back
Assuming headers object changes in place
Confusing HttpHeaders with plain objects
5. You want to send a GET request with headers { 'Authorization': 'Bearer abc123' } and query params { 'filter': 'active', 'limit': '10' }. Which code correctly sets both headers and params in Angular?
hard
A. const headers = new HttpHeaders({ Authorization: 'Bearer abc123' });
const params = new HttpParams({ fromObject: { filter: 'active', limit: '10' } });
this.http.get('/api/users', { headers, params }).subscribe();