CHAPTER 14
Beginner
Angular HTTP Client and APIs
Updated: May 18, 2026
5 min read
# CHAPTER 14
Angular HTTP Client and APIs
1. Chapter Introduction
Real applications consume data from servers. Angular'sHttpClient module provides a clean, Observable-based API for making HTTP requests. When combined with services and RxJS, it becomes a powerful data layer that seamlessly integrates into your component architecture.
2. Learning Objectives
-
Set up
HttpClientin an Angular application.
- Make GET, POST, PUT, and DELETE requests.
-
Handle HTTP errors gracefully using
catchError.
- Use TypeScript interfaces to type API responses.
- Build a Weather Application.
3. Setting Up HttpClient
app.config.ts
4. Making a GET Request
weather.service.ts
weather.component.ts
html
5. POST, PUT, DELETE Requests
typescript
6. Error Handling with catchError
typescript
7. HTTP Headers and Auth Tokens
typescript
8. Common Mistakes
- Subscribing in the service instead of the component: Services should return Observables, not subscribe to them. Components decide when to subscribe.
-
Not unsubscribing: Always unsubscribe from HTTP Observables in
ngOnDestroyor use theasyncpipe to handle subscriptions automatically.
9. MCQs with Answers
Question 1
What Angular module provides the HttpClient service?
Question 2
What does http.get<WeatherData>(url) return?
Question 3
What method is called on an Observable to start receiving data?
Question 4
In the .subscribe() method, what callback handles success?
Question 5
In the .subscribe() method, what callback handles errors?
Question 6
Which RxJS operator is used to catch and handle HTTP errors in a pipe?
Question 7
Which HTTP method is used to CREATE a new resource?
Question 8
Which HTTP method is used to completely REPLACE an existing resource?
Question 9
How do you add an Authorization header to an HTTP request?
Question 10
Why should API calls be made in Services rather than directly in components?
10. Interview Questions
- Q: What is the difference between a Promise and an Observable? Why does Angular's HttpClient use Observables?
- Q: How would you retry a failed HTTP request 3 times before showing an error?
11. Summary
Angular'sHttpClient paired with RxJS Observables creates a declarative, powerful, and testable data layer. By keeping all HTTP calls inside services and using catchError for graceful error handling, Angular applications stay robust and user-friendly even when the network misbehaves.