NG.VII · Angular 22 practice question
Consider the following Angular code snippet where an `HttpParams` object is used to build a request URL for a service call. ```typescript import { HttpParams } from '@angular/common/http'; const params = new HttpParams() .set('query', 'search term with spaces') .set('page', '2'); // Assuming HttpClient.get() is called with the URL and the params object // HttpClient.get('https://api.example.com/data', { params: params }); ``` What will the resulting query string effectively look like when sent to the server?
Check answer and explanation
Correct answer: `?query=search%20term%20with%20spaces&page=2`
Explanation
HttpParams automatically handles URL encoding for parameter values. Spaces in the value of the 'query' parameter will be encoded as `%20` (or sometimes `+`, but standard Angular/browser behavior for `HttpParams` leans towards percent-encoding for robustness).
Common trap
Many developers might mistakenly assume the space will be replaced by a plus sign (`+`) as sometimes seen in basic URL encoding, or they might forget that `HttpParams` handles this automatically, leading to option A. Option B might be correct in some older contexts but Option C represents the standard, safest, and explicit encoding provided by the Angular HTTP client.
Source: HTTP Client documentation (specifically regarding parameter handling) at https://angular.dev/guide/http
Checked against the cited text on 2026-11-27.