The new HTTP client introduced in Angular 4.3.1 introduces HTTP_INTERCEPTORS module that helps us to easily intercept outgoing HTTP requests sent from our application or incoming responses to our application.
To implement an HTTP Interceptor for our Angular App, we simply create a service that implements HttpInterceptor interface
import { Injectable } from'@angular/core';import { HttpRequest, HttpResponse, HttpInterceptor, HttpEvent, HttpHandler, HttpErrorResponse } from'@angular/common/http';import { Observable } from'rxjs/Observable';@Injectable()exportclassAppHttpInterceptorimplementsHttpInterceptor {intercept(request:HttpRequest<any>, next:HttpHandler):Observable<HttpEvent<any>> {constappRequestHeaders=newHttpHeaders({...request.headers,'Content-Type':'application/json','x-api-key':AppSettings.API_KEY });constappRequestParams=new params: req.params.set('filter','completed');//we have to clone the original HttpRequest object as its immutableconsthttpRequest=request.clone({ headers: appRequestHeaders });return next.handle(httpRequest).do((evt:HttpEvent<any>) => { if (evt instanceofHttpResponse) {//handle response hereconsole.log('response status=:',evt.status); } }).catch(response => {if (response instanceofHttpErrorResponse) {//handle response error here }returnObservable.throw(response); }); }}