HttpClient fundamentals

Intermediate · 39 min

provideHttpClient, typed requests, and the observables HttpClient hands back for every call.

Before thisDependency injection fundamentals and the injector treeObservable fundamentals for Angular

A Coder's Journey · 39 min

HttpClient fundamentals

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by A Coder's JourneyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `provideHttpClient()` registers HttpClient with the injector — the same provider-registration pattern every other platform feature in this roadmap has used.
  • Every HttpClient method returns an observable, not a promise — which means, exactly as covered in the observables lesson, nothing is actually sent until something subscribes.
  • Passing a type parameter (`http.get<User>(url)`) doesn't validate the response at runtime — it tells the compiler what shape to assume, which is only as correct as your assumption about what the server actually sends back.

The browser already has fetch — why does Angular ship its own?

Every browser already has fetch. HttpClient isn’t replacing it so much as wrapping it with things a real app ends up wanting consistently: a return type (observables) that composes with the rest of Angular’s reactive ecosystem, automatic JSON parsing, a built-in interceptor pipeline for cross-cutting concerns (covered next lesson), and typed responses that play well with TypeScript. You could build all of that yourself around fetch — HttpClient exists so every Angular app doesn’t have to.

Registering it, and the basic verbs

// app.config.ts
export const appConfig: ApplicationConfig = {
  providers: [provideHttpClient(), /* ... */],
};
private http = inject(HttpClient);

getUsers(): Observable<User[]> {
  return this.http.get<User[]>('/api/users');
}

createUser(user: Partial<User>): Observable<User> {
  return this.http.post<User>('/api/users', user);
}

provideHttpClient() is the same provider-registration pattern you’ve now seen for the router and for forms-adjacent features — one line in app.config.ts, and HttpClient becomes injectable anywhere in the app. .get(), .post(), .put(), .delete() map directly onto the HTTP verbs you’d expect, each accepting a type parameter describing what shape the response should be assumed to have.

Observables, not promises — and why that’s not incidental

getUsers(): Observable<User[]> {
  return this.http.get<User[]>('/api/users'); // nothing sent yet
}

// somewhere else:
this.userService.getUsers().subscribe((users) => (this.users = users)); // now it fires

This is exactly the lazy-by-default behavior the observables lesson covered, and it’s worth re-grounding here because HttpClient is where it first has real consequences: calling http.get(...) builds an observable describing the request, but sends nothing over the network until something subscribes. Forget to subscribe (or let the async pipe do it for you), and the request simply never happens — no error, no network activity, nothing. Coming from fetch, where calling the function immediately fires the request, this trips people up constantly in their first few weeks with HttpClient.

The observable return type also means everything from the RxJS section of this roadmap — switchMap, catchError, retry — applies directly to HTTP calls, which is exactly why that material came before this lesson rather than after it.

Typed, but not validated

getUser(id: string): Observable<User> {
  return this.http.get<User>(`/api/users/${id}`);
}

The <User> here is purely a compile-time annotation — it tells TypeScript “assume whatever comes back has this shape,” and TypeScript will happily let you write code against that assumption. It performs no runtime check against the actual response body. If the server’s real shape drifts from what you assumed — a renamed field, an extra wrapper layer — nothing catches that at the point of the request; it surfaces later, wherever code tries to read a property that isn’t actually there. Keeping the type parameter honestly in sync with what the API genuinely returns is a discipline, not something the compiler enforces for you here.

This lesson’s video builds real API communication into a from-scratch standalone app as part of an ongoing series, with enough length to cover GET and POST in realistic depth rather than a toy example — the linked “deeper” video focuses specifically on wrapping HTTP calls in a clean, reusable service.

Check yourself

  1. 01What do you need in `app.config.ts` before `HttpClient` can be injected anywhere in the app?

    `provideHttpClient()` added to the `providers` array — the same registration step every platform feature (the router, forms) needed in earlier lessons. Without it, injecting `HttpClient` fails, because nothing has told the injector how to construct one.

  2. 02You write `this.http.get<User>('/api/me').subscribe(user => this.user = user)`, but the endpoint actually returns `{ data: { name: ..., email: ... } }` — a wrapper object, not a bare User. Does the `<User>` type parameter catch this mismatch?

    No. The type parameter only tells TypeScript what shape to *assume* the response has — it performs no runtime validation against the actual response body. If the real shape doesn't match, you get a value TypeScript believes is a `User` but which is actually the wrapper object, and the mismatch only surfaces when code tries to read a property that doesn't exist where expected.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark