NG.VII · Angular 22 practice question
When constructing an HTTP GET request in Angular, you need to pass multiple query parameters. Which approach ensures that the parameters are correctly URL-encoded, especially if the values contain special characters like spaces or ampersands, while leveraging Angular's built-in HttpClient functionality?
Check answer and explanation
Correct answer: Use the `HttpParams` class and chain `.set()` methods to build the parameters object, which the HttpClient handles during serialization.
Explanation
The `HttpParams` class is designed specifically to manage query parameters for HTTP requests in Angular. When passed to the HttpClient, it correctly handles the serialization and URL encoding of the provided keys and values, which is cleaner and more robust than manual string manipulation.
Common trap
Option A and D are risky because manual encoding can be complex to get perfectly right for all edge cases, whereas using `HttpParams` delegates this responsibility to the framework. Option C is incorrect because the standard way to pass query parameters is via the `params` option, which typically expects an `HttpParams` object or a similar structure.
Source: Angular HTTP Client documentation (e.g., "Sending HTTP requests" section) - https://angular.dev/guide/http
Checked against the cited text on 2026-11-27.