Angular HttpClient

In this section, we will learn about the most important aspect of Angular—HttpClient. Using the HttpClient interface, we can perform HTTP request and response calls. In the previous chapter, we learned about dependency injection and services; in this chapter, we will learn how to write services, which will include methods so that we can make HTTP calls and process responses using HttpClient.

HttpClient is a small, easy-to-use, powerful library for performing HTTP request and response calls. Using HttpClient, we can easily communicate with backend services, and the module supports most modern web browsers. HttpClient comes with a lot of advanced functionality, such as interceptors and progress events. HttpClient supports various HTTP methods, including GET, POST, PUT, PATCH, DELETE, JSONP, and options. Each of these calls always returns an observable. We have to subscribe to the observables in order to process the responses. If we do not subscribe, nothing will happen. 

HttpClientModule is available in the @angular/common/http library and needs to be imported into the app.module.ts file; otherwise, we will encounter errors.

We now know about the HttpClient module, but before we jump into implementing the module in our applications, it's good to know about some of the key functionality that got added to HttpClient

  • HttpClient provides a strongly typed response body.
  • The request/response objects in HttpClient are immutable.
  • The JSON format response is the default. We no longer have to map it into a JSON object.
  • HttpClient provides interceptors that are really helpful in middleware for intercepting an HttpRequest for transforming or processing a response.
  • HttpClient includes testability features. We can easily mock the requests and process headers more efficiently. 

In the following section, we will learn about the HttpClient module, which needs to be imported into the component or service where we can make the HTTP calls. We will also learn about the HTTP verbs that are available and learn about their purpose in modern applications.

..................Content has been hidden....................

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