Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are advantages of Angular 5 HttpClient over previous Http?

I read official upgrade guide and it says something like "because HttpClient gets wide adoption we decided to..." but what are real benefits this HttpClient brings along?

I was considering to try it out, but got confused half way as I don't know what needs to happen to these after I upgrade:

import { Http, Headers, RequestOptions, ResponseContentType } from '@angular/http';

I tried to find a "match in the new client: import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http'

But have no idea how those should be properly "upgraded" as the guide says nothing about it.

So second question - what do we do with those other http things?

like image 389
Sergey Rudenko Avatar asked Mar 08 '23 07:03

Sergey Rudenko


1 Answers

The HttpClient interface is pretty much unchanged from the old Http interface. The big difference is that HttpClientModule has better support for middleware (i.e .HTTP interceptors).

Some of the feature benefits:

•   Strongly typed response body access
•   JSON assumed by default (no more need to do .map(t=>t.json())
•   Better support for interceptors as middleware
•   Immutable request/response objects
•   Progress events for request upload/response download

With the new http client, its straight-forward to use HTTP interceptors for middleware components.

Some benefits of the middleware pattern:

  1. Global error handling for HTTP requests
  2. Global retry mechanism (i.e. retry 3 times)
  3. Global HTTP spinner for long-running operation.

For example, client code (using HttpClient) does not need to worry about error handling, retries, or visual wait components – all of that is handled in one place.

like image 160
pixelbits Avatar answered Apr 07 '23 13:04

pixelbits