auth.interceptor.ts
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; export class AuthInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const reqCopied = req.clone({ headers: req.headers.append('', ''), params: req.params.set('', '') }); return next.handle(reqCopied); } }
app.module.ts
import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angular/core'; import {FormsModule} from '@angular/forms'; import {AppComponent} from './app.component'; import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http'; import {AuthInterceptor} from './auth.interceptor'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, FormsModule, HttpClientModule ], providers: [ {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true} ], bootstrap: [AppComponent] }) export class AppModule { }
References
https://angular.io/guide/http#intercepting-requests-and-responses