1

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 it is working for me :

@Injectable()
export class HttpRequest extends RequestOptions {
    constructor() {
         super({
             method: RequestMethod.Get,
             headers: new Headers({
                'X-Some-Header': 'some-content'})
        });
    }
}

But for overriding Response I have problem :

@Injectable()
export class HttpResponse extends Response {
    constructor(responseOptions: ResponseOptions) {
        super(responseOptions);
        console.log("RESPONSE IS CREATED!!!!!!!");
    }
}

The constructor never being called and here it is the bootstrap:

bootstrap(AppComponent,
    [   ROUTER_PROVIDERS
        ,HTTP_PROVIDERS
        ,provide(RequestOptions, {useClass: HttpRequest})
        ,provide(Response, {useClass: HttpResponse})
    ]);

The reason for overriding the response is to read some response headers and control for 404-Not Page Found globally and ....

Thanks,

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So whats difference between request and response, for each http call we have to create new request! also injector should be able to create new instance as well. Am I wrong ?
Injectors maintain singletons. It'd doable injecting factories but apparently that way wasn't choosen. A request just fires away and can be reused for later requests, a response might be around much longer because the reference is passed to your code and it's up to you how long you keep it around and read from it. It's out of Angulars control. Subsequent requests that would reuse this response instance would override each others data.
I can't understand it yet, but anyway another solution would be overriding Http ?
Yes, another solution would be overriding this default behavior by extending Http.
0

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 })
]);

3 Comments

As i said, you can capture response by adding listener directly to underlying XMLHttpRequest, and the same applies to custom headers.
is there any document or example for manipulating BrowserXhr, adding listener or ... ?
According to spec: You must call setRequestHeader() after open(), but before send(). So you should use Angular Http module api to set request headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.