In Angular 2 I am trying to control http request/response to set/read some headers when sending request and getting response.
I just override HttpRequest like this, and
You could implement it in a few ways: create base service class or provide custom xhr implementation:
@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
constructor() {}
build(): any {
let xhr:XMLHttpRequest = super.build();
/*...add headers, listeners etc...*/
return <any>(xhr);
}
}
bootstrap(AppComponent, [
HTTP_PROVIDERS,
provide(BrowserXhr, { useClass: CustomBrowserXhr })
]);
You can't override Response I remember seeing that it is being created using new Response in one of the related Http classes instead of requesting it from the injector, which is reasonable because Response needs to be a dufferent instance for each request.