Last active
January 4, 2018 16:04
-
-
Save cozzbie/03a02d05bcc0920a810f8fdde68115bb to your computer and use it in GitHub Desktop.
AngularJS interceptor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Injector } from '@angular/core'; | |
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; | |
import { Observable } from 'rxjs/Rx'; | |
import 'rxjs/add/observable/throw' | |
import 'rxjs/add/operator/catch'; | |
@Injectable() | |
export class MyHttpInterceptor implements HttpInterceptor { | |
constructor() { } | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
console.log("intercepted request ... "); | |
// Clone the request to add the new header. | |
const authReq = req.clone({ headers: req.headers.set("headerName", "headerValue")}); | |
console.log("Sending request with new header now ..."); | |
//send the newly created request | |
return next.handle(authReq) | |
.catch((error, caught) => { | |
return Observable.throw(error); | |
}) as any; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment