HttpClient (@angular/common/http) performs REST calls and returns Observables. Register it with provideHttpClient() at bootstrap in CLI apps.
Typical flow
- Inject
HttpClientin a service - Call
get<User[]>('/api/users') - Subscribe in the service or expose to templates via
asyncpipe - Map HTTP errors to user-visible state—never fail silently
Interceptors
Interceptors attach auth headers, log requests, or retry failures across the app without duplicating code in every service.
Important interview questions and answers
- Q: Why Observables instead of Promises only?
A: Cancellation, composition with RxJS operators, and multiple emissions (progress events). - Q: Where handle 401 responses?
A: Often in an interceptor that redirects to login or refreshes tokens.
Self-check
- What does the playground log for a GET users call?
- Why pair HttpClient with the async pipe in templates?
Challenge
HTTP shape
- Run the default code.
- Add a second
printOutputdescribing error handling.
Done when: terminal shows the Observable-based GET description.
Pitfall: Forgetting error handling in subscribe—always map HTTP failures to user-visible state.
Interview prep
- What does HttpClient return?
Observables of HTTP events/bodies—compose with RxJS operators and unsubscribe via async pipe or takeUntil.