简体   繁体   中英

How can I set module specific custom headers on a HTTP request?

I would like to be able to set a custom header on the HTTP requests my application triggers. This header must indicate from which submodule the request has been fired.

I already tried creating an interceptor and a decorator (in each module) but both options are placed on the global angular $http service and thus overriding eachother.

The only option that works is specifying the header when calling the $http service but that is not really what I want. The goal is to write a interceptor or decorator on each module that catches all requests from that module, setting a header accordingly.

Any ideas on how to solve this?

Pretty sure this can't be solved just by separation using modules since as you've found the $http service is in the 'ng' module which is automatically included. When you inject $http you are getting the same instance that's defined in the 'ng' built-in module.

Your best bet is probably to write base classes that you use for two factory/service objects that each abstract away the actual calls and set the appropriate headers. Nice basis for an API abstraction layer implementation here https://gist.github.com/jelbourn/6276338

what you can do is (what I had done) prefixed all the api paths with module names eg 'stk-api/abc/get', 'fu-api/xyz/get', etc.. and then in interceptor change the api path to remove the module part 'stk-' or 'fu-' and added the custom header as per the removed part or module name..

I know its a workaround kind of solution, but did the job for me...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM