Skip to content

Instantly share code, notes, and snippets.

@nicowernli
Last active March 28, 2019 11:39
Show Gist options
  • Save nicowernli/c097a37a6f05552cc5a9a48b4e945e07 to your computer and use it in GitHub Desktop.
Save nicowernli/c097a37a6f05552cc5a9a48b4e945e07 to your computer and use it in GitHub Desktop.
Simple HttpInterceptor that adds a header to a request
import { Injectable } from '@angular/core';
import {HttpHandler, HttpRequest, HttpInterceptor} from '@angular/common/http';
import {environment} from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class BaseURLInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (!req.url.match(/^http(s)?:\/\/(.*)$/)) {
const url = `${environment.baseURL}${req.url}`.replace(/([^:]\/)\/+/g, '$1');
req = req.clone({ url });
}
return next.handle(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment