4
votes

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?

1

1 Answers

7
votes

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.