Angular HttpClient vs HttpClientModule

What Is HttpClient in Angular?

What Is HttpClient in Angular?
What Is the role and responsibility of HttpClient?
HttpClient is performing HTTP requests and responses.

Most of all web applications communicate with backend services over the HTTP protocol and all modern browsers support two different APIs for making HTTP requests i.e.
1.      XMLHttpRequest interface
2.      fetch() APIs

The HttpClient is more modern and easy to use the alternative of HTTP.

HttpClient is an improved replacement for HTTP. They expect to deprecate http in Angular 5 and remove it in a later version.  

The new HttpClient service is included in the HTTP Client Module that used to initiate HTTP request and responses in angular apps. The HttpClientModule is a replacement of HttpModule.

HttpClient also gives us advanced functionality like the ability to listen for progress events and interceptors to modify requests or responses.

Before using the HttpClient, you must need to import the Angular HttpClientModule and the HttpClientModule is imported from @angular/common/http.

You must import HttpClientModule after BrowserModule in your angular apps.

First, you’ll need to imported HttpClientModule from @angular/common/http in your app module and it must be import HttpClientModule after BrowserModule in your angular apps.

The following example as given below -
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

//Import App Component
import { AppComponent } from './app.component';

//AppModule class with @NgModule decorator
  imports: [
    //import HttpClientModule after BrowserModule
  //Static, compiler configuration
  //declarations is used for configure the selectors
  declarations: [
  //Runtime or injector configuration
  providers: [],
  //bootstrapped entry component
  bootstrap: [ AppComponent ]
export class AppModule {}

After imported HttpClientModule into the AppModule, you can inject the HttpClient into your created service.

The following example as give below–
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

export class CustomerService {
  //Inject HttpClient into your components or services
  constructor(private httpClient: HttpClient) { }


HttpClient supports mutating a request, which is, sending data to the server with HTTP methods such as PUT, POST, and DELETE.

HttpClient is use the XMLHttpRequest browser API to execute HTTP request.

HttpClient Perform HTTP requests –
1.      GET method – get()
2.      POST method – post()
3.      PUT method – put()
4.      DELETE method – delete()
5.      HEAD method – head()
6.      JSONP method – jsonp()
7.      OPTIONS method – options()
8.      PATCH method – patch()
And the HttpClient class looks like –
class HttpClient {
  constructor(handler: HttpHandler)
  request(first: string | HttpRequest<any>, url?: string, options: {...}): Observable<any>
  delete(url: string, options: {...}): Observable<any>
  get(url: string, options: {...}): Observable<any>
  head(url: string, options: {...}): Observable<any>
  jsonp<T>(url: string, callbackParam: string): Observable<T>
  options(url: string, options: {...}): Observable<any>
  patch(url: string, body: any | null, options: {...}): Observable<any>
  post(url: string, body: any | null, options: {...}): Observable<any>
  put(url: string, body: any | null, options: {...}): Observable<any>

The options contain the list of parameters –
1.      headers
2.      observe,
3.      params,
4.      reportProgress,
5.      responseType,
6.      withCredentials

The following options parameters look like -
  options: {
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    observe?: 'body';
    params?: HttpParams | {
        [param: string]: string | string[];
    reportProgress?: boolean;
    responseType: 'blob';
    withCredentials?: boolean;

Benefits of HttpClient -
1.      HttpClient include the testability features
2.      HttpClient include typed request
3.      HttpClient include response objects
4.      HttpClient include request and response interception
5.      HttpClient include Observable APIs
6.      HttpClient include error handling

For more detail kindly refer the link.... 

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog -
My Books - Book 1 and Book 2

What Is HttpClient in Angular? What Is HttpClient in Angular? Reviewed by Anil Singh on 11:57 PM Rating: (5) Powered by Blogger.