0
0
Angularframework~20 mins

Setting headers and params in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Http Headers & Params Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What headers and params are sent in this Angular HTTP request?

Consider this Angular service method that makes an HTTP GET request:

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';

getData() {
  const headers = new HttpHeaders().set('Authorization', 'Bearer token123');
  const params = new HttpParams().set('page', '2').set('limit', '10');
  return this.http.get('/api/items', { headers, params });
}

Which headers and query parameters will be included in the request?

AHeaders: Authorization=Bearer token123; No params sent
BHeaders: Authorization=Bearer token123; Params: page=2, limit=10
CHeaders: Authorization=Bearer token123; Params: page=10, limit=2
DHeaders: Authorization=token123; Params: page=2, limit=10
Attempts:
2 left
💡 Hint

Look at how HttpHeaders and HttpParams are set with set method.

📝 Syntax
intermediate
2:00remaining
Which option correctly adds multiple headers in Angular HttpHeaders?

Given the need to add two headers: 'Content-Type' as 'application/json' and 'Accept' as 'application/json', which code snippet correctly creates the HttpHeaders object?

Aconst headers = new HttpHeaders().set('Content-Type', 'application/json').set('Accept', 'application/json');
Bconst headers = new HttpHeaders({'Content-Type': 'application/json', 'Accept': 'application/json'});
Cconst headers = new HttpHeaders().append('Content-Type', 'application/json').append('Accept', 'application/json');
Dconst headers = new HttpHeaders().set('Content-Type', 'application/json'); headers.set('Accept', 'application/json');
Attempts:
2 left
💡 Hint

Remember that HttpHeaders is immutable and methods return new instances.

🔧 Debug
advanced
2:00remaining
Why does this Angular HTTP request not send the expected query parameters?

Look at this code snippet:

let params = new HttpParams();
params.set('search', 'books');
params.set('sort', 'asc');
this.http.get('/api/products', { params }).subscribe();

Why are the query parameters missing in the request?

ABecause the <code>get</code> method requires headers to send params.
BBecause <code>HttpParams</code> does not support multiple parameters.
CBecause <code>HttpParams</code> is immutable; <code>set</code> returns a new instance but the code ignores it.
DBecause the <code>subscribe</code> method must be called before setting params.
Attempts:
2 left
💡 Hint

Check how HttpParams methods work regarding immutability.

state_output
advanced
2:00remaining
What is the final value of headers after this Angular code runs?

Consider this code:

let headers = new HttpHeaders();
headers = headers.set('X-Custom', '123');
headers.set('Authorization', 'token');

What headers will be sent if headers is used in an HTTP request?

AHeaders: X-Custom=123 only
BHeaders: X-Custom=123 and Authorization=token
CHeaders: Authorization=token only
DNo headers sent
Attempts:
2 left
💡 Hint

Remember that HttpHeaders methods return new instances and do not mutate the original.

🧠 Conceptual
expert
3:00remaining
How does Angular's HttpParams handle multiple values for the same key?

Which statement best describes how Angular's HttpParams manages multiple values for the same query parameter key?

A<code>HttpParams</code> merges multiple values into a comma-separated string automatically.
B<code>HttpParams</code> throws an error if you try to add multiple values for the same key.
C<code>HttpParams</code> overwrites previous values for a key; multiple values are not supported.
D<code>HttpParams</code> allows multiple values per key using <code>append</code>, resulting in repeated keys in the URL.
Attempts:
2 left
💡 Hint

Think about how to add multiple values for the same parameter in a URL query string.