Full answer
HttpClient is Angular's built-in tool for calling REST APIs. In a standalone app, add provideHttpClient() to the application config. In an NgModule app (legacy) you imported HttpClientModule instead, which is deprecated.
Calls return Observables:
this.http.get<Course[]>('/api/courses');The type argument only tells TypeScript the expected shape. It does not check the data at runtime. If the server sends something different, the compiler cannot warn you.
The request is sent when something subscribes, and each subscription sends a new request. In a template, the async pipe subscribes and unsubscribes for you. The Observable completes after the response, so there is no leak from one-shot HTTP calls.
Error handling. A failed request errors the Observable with an HttpErrorResponse. Use catchError to show a message or return a fallback value: catchError(() => of([])). Status code 0 means the request never reached a server (network, CORS, or offline).
Good habits: keep HTTP calls in a service, not in the component; set the API base URL in one place; and use withFetch() in provideHttpClient if you use server-side rendering, as it uses the Fetch API.
Other verbs: post<T>(url, body), put, patch, delete. Query parameters go in { params: { page: 2 } }, headers in { headers: {...} }.
// file: course.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable, catchError, of } from 'rxjs';
export interface Course {
id: number;
title: string;
}
@Injectable({ providedIn: 'root' })
export class CourseService {
private readonly http = inject(HttpClient);
list(): Observable<Course[]> {
return this.http.get<Course[]>('/api/courses').pipe(
catchError((err) => {
console.error('Could not load courses', err);
return of([]);
}),
);
}
}
// file: course-list.component.ts
import { AsyncPipe } from '@angular/common';
import { Component, inject } from '@angular/core';
import { CourseService } from './course.service';
@Component({
selector: 'app-course-list',
imports: [AsyncPipe],
template: `
@if (courses$ | async; as courses) {
<ul>
@for (c of courses; track c.id) {
<li>{{ c.title }}</li>
} @empty {
<li>No courses found</li>
}
</ul>
} @else {
<p>Loading...</p>
}
`,
})
export class CourseListComponent {
readonly courses$ = inject(CourseService).list();
}
// file: main.ts
import { provideHttpClient, withFetch } from '@angular/common/http';
import { bootstrapApplication } from '@angular/platform-browser';
import { CourseListComponent } from './course-list.component';
bootstrapApplication(CourseListComponent, {
providers: [provideHttpClient(withFetch())],
}).catch((err) => console.error(err));Angular 20.3. Replace the URL with a real endpoint. The code has a service, a component, and the config in one file set.
Key points
- `provideHttpClient()` enables HttpClient. `HttpClientModule` is legacy.
- Calls are cold Observables, so nothing is sent until you subscribe.
- The generic type is a compile-time hint, not runtime validation.
- Handle failures with `catchError`, and keep calls inside a service.
Common mistakes
- Calling `http.get(...)` and forgetting to subscribe, so no request is sent.
- Subscribing twice (for example the same observable with two `async` pipes) and sending two requests.
- Trusting the generic type as proof the server data is valid.
Follow-ups
- How would you avoid sending the same request twice from one template?
- What does an interceptor do?
- How do you cancel an in-flight request?
Tips
- Say that the generic does not validate data. Many candidates miss this.
- Mention status 0 and what it usually means.
- Offer `withFetch()` if SSR comes up.
Evaluation rubric
- Registers HttpClient with provideHttpClient.
- Understands cold observables and subscription.
- Handles errors without hiding them.