Angular - Failing to intercept http requests using a http service

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Angular - Failing to intercept http requests using a http service



I have an angular 6 application.



I am assigning a HTTP interceptor to a specific lazy loaded module. When i make http calls directly in the component using http client, the requests are intercepted. However, making http calls via a core module service, it will fail to intercept these http requests.



See the following example.



https://stackblitz.com/edit/angular-ckzdvx?file=src%2Fapp%2Fa%2Ffirst%2Ffirst.component.ts



first.component.ts


import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ApiService } from './../../core/api.service';

@Component({
selector: 'app-first',
templateUrl: './first.component.html',
styleUrls: ['./first.component.css']
})
export class FirstComponent implements OnInit {

constructor(private http: HttpClient, private apiService: ApiService) { }

ngOnInit() {
}

call() {
this.http.get("https://jsonplaceholder.typicode.com/users").subscribe((res) => {
console.log(res);
})
}

call2() {

this.apiService.get("https://jsonplaceholder.typicode.com/users").subscribe((res) => {
console.log(res);

});

}

}



I am making 2 calls, the first call() is intercepted, the second call2() is not being intercepted.


call()


call2()



Outcome



call2() should be intercepted whilst using the api service.


call2()



It's important that the http interceptor is declared only in the module it is needed in. As i don't want other modules to have their HTTP requests intercepted.





probaly apiservice is using different HTTPclient without interceptors.
– Antoniossss
29 mins ago





Yes but why? and what is the solution here :)
– KHAN
14 mins ago





Didnt see the code. Isnt Swilkos answer sufficient?
– Antoniossss
11 mins ago





No it is not sufficient because, i only want to declare the interceptor in a specific module. I don't want other modules to be intercepted, if i declare it in the app.module.ts all modules using http requests are going to be intercepted. Please refer to the stackblitz for the code.
– KHAN
10 mins ago







So what is required outcome? - this is totally unclear - use specific module names etc
– Antoniossss
9 mins ago






2 Answers
2



I think you need to add the following provider to your app.module.ts to hit all requests


app.module.ts


import { HttpClientModule, HTTP_INTERCEPTORS } from
'@angular/common/http';

import { MyInterceptor} from './my.interceptor';

@NgModule({
imports: [BrowserModule, FormsModule, AppRoutingModule,
CoreModule.forRoot(),
HttpClientModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: MyInterceptor,
multi: true
}
})
export class AppModule { }





Please read the last sentence of my post. I mentioned i want to declare the interceptor in a specific module so other modules are not intercepted. If i declare it in the app.module.ts it is doing exactly opposite of what i want.
– KHAN
16 mins ago





The reason call2() is not hitting your interceptor is: it is not using FirstComponent's injected HttpClient. It is using ApiService instead. That instance of ApiService has its own HttpClient injected in the constructor and that HttpClient doesn't provide MyInterceptor as an HTTP_INTERCEPTOR.


call2()


FirstComponent


HttpClient


ApiService


ApiService


HttpClient


HttpClient


MyInterceptor


HTTP_INTERCEPTOR



Changing that second call to this.http.get will make it hit your interceptor.


this.http.get





I want to use the api service though, thats the whole point. So i need it to be intercepted whilst using the api service.
– KHAN
7 mins ago







You probably should have mentioned that in the original version of the question. You asked why it wasn't being intercepted. I answered that question and explained how dependency injection works and why your call wasn't being intercepted.
– Dean
27 secs ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size