gund / ng-http-interceptor

Http Interceptor library for Angular
MIT License
104 stars 16 forks source link

Interceptors not running #162

Open will093 opened 7 years ago

will093 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

Yugloocamai commented 7 years ago

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

gund commented 7 years ago

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?