Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Interceptors not running #162

Open
will093 opened this issue Jul 21, 2017 · 8 comments
Open

Interceptors not running #162

will093 opened this issue Jul 21, 2017 · 8 comments

Comments

@will093
Copy link

will093 commented Jul 21, 2017

I have imported HttpModule and HttpInterceptorModule, and created an interceptor service:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { HttpInterceptorService } from 'ng-http-interceptor';

@Injectable()
export class HttpErrorResponseInterceptor {

  constructor(private http: Http, private httpInterceptor: HttpInterceptorService) { }

  initialise() {

    this.httpInterceptor.request().addInterceptor((data, method) => {
      console.log(method, data);
      return data;
    });

    this.httpInterceptor.response().addInterceptor((res, method) => {
      return res.do(r => console.log(method, r));
    });

    this.http.get('/')
      .subscribe(console.log);
  }
}

initialise() is then called in the top level AppComponent.

When I debug in Chrome/IE/Firefox I can see http.get is called - but the interceptors never run, and there are no errors. I have tried injecting InterceptableHttp instead of Http, and the interceptors still don't run.

I am using Angular 4, specifically, the versions I am using are:

"@angular/http": "^4.3.1",
"ng-http-interceptor": "^3.1.2",
@gund
Copy link
Owner

gund commented Jul 21, 2017

What is the order of imports of HttpModule and HttpInterceptorModulev in your module?

Because if you have HttpModule after HttpInterceptorModule it will override proxied Http service with original one.

@will093
Copy link
Author

will093 commented Jul 21, 2017

@gund The problem still occurs if I import HttpModule first. I was wondering if something like that was occurring, but then I would think InterceptableHttp should still work, which it doesn't.

@gund
Copy link
Owner

gund commented Jul 21, 2017

Yeah you are right, InterceptableHttp should have been worked any way...
It's weird why requests are not intercepted, can you please make a plunkr with this issue reproduced?

@davorpeic
Copy link

I have the same setup as you, and I was using HttpInterceptorModule.noOverrideHttp() and I didn't get any logs, I switched to normal module HttpInterceptorModule and now I can see all requests. (Ionic 3 app).

@will093
Copy link
Author

will093 commented Jul 28, 2017

I have identified the cause of the issue. Another plugin I am using in my project seems to provide its own HTTP implementation via a factory:

https://github.com/aitboudad/ng-loading-bar

As each plugin provides its own implementation of HTTP I think the 2 plugins are incompatible.

@gund
Copy link
Owner

gund commented Jul 28, 2017

Yeah, only the last Http provided will be used. But you should be able to still use InterceptableHttp to make interceptable requests.

@Yugloocamai
Copy link

I really need to be able to intercept ALL Http requests. Everything throughout my entire app lifecycle, including all DI.

@gund
Copy link
Owner

gund commented Aug 15, 2017

This is a really interesting case when you really want to compose your DI providers but not completely override them.

So far I don't know any solution of how to achieve this but it definitely makes sense in such scenarios.

Maybe we should open an issue in official Angular repo and discuss this problem/feature...

What do you guys think?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants