Performance: Setting headers and params
This affects the network request time and how quickly the browser can start processing data from the server.
const headers = new HttpHeaders().set('Authorization', 'Bearer token'); const params = new HttpParams().set('filter', 'all'); this.http.get('/api/data', { headers, params })
this.http.get('/api/data', { headers: new HttpHeaders({ 'Authorization': 'Bearer token', 'X-Custom-Header': 'value', 'Unused-Header': 'remove' }), params: new HttpParams().set('filter', 'all').set('unused', 'remove') })
| Pattern | Request Size | Network Latency | Server Processing | Verdict |
|---|---|---|---|---|
| Unoptimized headers and params | Large (extra headers/params) | Higher due to size | Longer due to extra data | [X] Bad |
| Optimized headers and params | Minimal necessary data | Lower due to smaller size | Faster processing | [OK] Good |
Authorization with value Bearer token123 using Angular's HttpHeaders?set method to add headers.new HttpHeaders().set() correctly; others are invalid syntax or methods.const params = new HttpParams().set('page', '2').set('sort', 'name');
this.http.get('/api/items', { params }).subscribe();set returns a new HttpParams with added param; chaining adds both params.page=2 then sort=name.const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json');
this.http.post('/api/data', body, { headers }).subscribe();set return new instances; original is unchanged.set result is not assigned back.{ 'Authorization': 'Bearer abc123' } and query params { 'filter': 'active', 'limit': '10' }. Which code correctly sets both headers and params in Angular?{ Authorization: 'Bearer abc123' } for initial headers.{ fromObject: { filter: 'active', limit: '10' } } to set multiple params at once.headers and params are passed correctly in the request options { headers, params }.Pass the test and this pattern moves unknown