HttpClient and HTTP verbs

If the previous section was an introduction to HttpClientModule and HttpClient and its advantages, in this section, we will dig deeper and also learn how to write some sample code for implementing HttpClient.

As we mentioned earlier, HttpClient supports GET, POST, PUT, PATCH, DELETE, JSONP, and options methods, which will return observables. HttpClient also provides modules, which can easily pass various options and data using HttpHeaders and HttpParams.

In order to use HttpClient, we will need to import HttpClientModule into our application module (app.module.ts) file, and we also need to import HttpClient into our services or components and inject HttpClient inside the constructor so that we can use it to make HTTP calls. Add the following line of code to your app.module.ts file, and don't forget to add it to the list of import modules as well:

// Import the module into the component or service
import { HttpClient } from '@angular/core/http';

// Inside the constructor method inject the HttpClient and create an instance
constructor(private http: HttpClient)

Now, let's implement some of the most frequently used HTTP verbs.

We will implement the HTTP methods separately for both the JSON server APIs and the Firestore database.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.192.151