Using the Angular Http Client in Angular v15

Netanel Basal
Netanel Basal
Published in
1 min readOct 25, 2022

--

As part of the new standalone feature, Angular v15 introduces a new method to use the Http client API that doesn’t require using HttpClientModule:

As with the Router API, the new API exposes a provideHttpClient function that takes the features we want to use. Using interceptors is now made easier with a new feature function called withInterceptors(). It accepts a list of interceptor functions we want to use:

If you still want to use the current interceptors without changing your code, you can provide the withInterceptorsFromDi() feature:

Interceptors in Lazy Loaded Modules

The ability to define interceptors in lazy-loaded modules has been requested for a long time. A new developer preview feature in this version of Angular makes this possible to do it. We can use the withRequestsMadeViaParent() feature in our lazy loaded routes and modules:

In the above case, each request initiated by the TodoComponent and its children will use both the inline and the global interceptors 🙌 (inline runs first)

In tests, we can use the provideHttpClientTesting() function.

Follow me on Medium or Twitter to read more about Angular and JS!

--

--

A FrontEnd Tech Lead, blogger, and open source maintainer. The founder of ngneat, husband and father.