Also the HttpClient
is use the XMLHttpRequest browser
API to execute HTTP request and it specific the HTTP request type’s i.e.

üGet()

üPost()

üPut()

üDelete()

üPatch()

üHead()

üJsonp()

Before use the HttpClient, we need to import in the HttpClientModule into your AppModule. Once you import HttpClientModule into your AppModule, you can inject HttpClient into your components and services.

HttpClientModule -

The HttpClientModule imported form -

import
{ HttpClientModule } from'@angular/common/http';

NgModule which provides the HttpClient
and associated with components services and the interceptors can be added to
the chain behind HttpClient by binding them to the multi provider for
HTTP_INTERCEPTORS.

HttpClient – The HttpClient is used to perform HTTP requests.

The HttpClient imported form -

import
{ HttpClient } from'@angular/common/http';

Example for the get () and post ()
method on HttpClient –

import { Component, OnInit } from'@angular/core';

import { HttpClient } from'@angular/common/http';

@Component({

selector:'app-root',

templateUrl:'./app.component.html',

styleUrls: ['./app.component.css']

})

exportclassAppComponentimplementsOnInit {

baseUrl ="https://code-sample.com/";

users = null;

// Inject HttpClient into your
component or service.

constructor(privatehttp: HttpClient){ }

//Load User info.

ngOnInit(): void {

// Make the HTTP
request:

this.http.get(this.baseUrl +'api/users/').subscribe(data=> {

this.users = data;

},

err=> {

console.log("Error-
something is wrong!")

});

}

addUser = function(){

letuser ={

id:1,

name:'Anil Singh',

user_Id:9979,

site :'https://code-sample.com'

}

//Make the HTTP Post Request

this.http.post(this.baseUrl +'api/addUser/', user)

.subscribe(

result=> {

console.log("The User
added successfully!");

console.log(result);

},

err=> {

console.log("Error-
something is wrong!")

});

}

}

I hope you are enjoying with this post!
Please share with you friends!! Thank you!!!