Home > Enterprise >  Angular How to only intercept Http calls made by certain service
Angular How to only intercept Http calls made by certain service

Time:08-25

Is there a way to only apply the angular http interceptor to calls in a certain shared module or for certain individual services?

CodePudding user response:

It's not possible to set up your own service based interceptors. However, you can pass data with each request and simply check in the interceptor for that condition.

You could pass like a query param or set a custom header for that request to be checked against.

    if (headers.has('my-custom-header')) {
      headers=headers.append('content-type','application/json')
    }

And then in the file where you are calling the request, you could pass in the custom header you are configuring

   let headers = new HttpHeaders()
    headers=headers.append('my-custom-header', 'true')

Here's also another more detailed answer https://stackoverflow.com/a/50625945/15492085

  • Related