Skip to content

Instantly share code, notes, and snippets.

@cozzbie
Last active January 4, 2018 16:04
Show Gist options
  • Save cozzbie/03a02d05bcc0920a810f8fdde68115bb to your computer and use it in GitHub Desktop.
Save cozzbie/03a02d05bcc0920a810f8fdde68115bb to your computer and use it in GitHub Desktop.
AngularJS interceptor
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