6

Is it possible to set a header only for specified domains?

The "normal" way of doing this adds that header for all calls, even calls that retrieve HTML templates for example.

$http.defaults.headers.common['Authorization']='...';

So far, other than using an $http interceptor, I can't see any other way, but if you can think of one, I'm curious.

Thanks.

Francisc
  • 77,430
  • 63
  • 180
  • 276

1 Answers1

8

Creating an interceptor is the way I handle this. I am not sure of another way, so, here's an example anyways. :)

You could create an interceptor that is registered with $httpProvider. Angular will pass the default config object for modification and you could modify the headers there.

Here if a quick example based on the Angular $http doc page:

//register interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
    return {
        // optional method
        'request': function(config) {
            //Figure out which headers you want and set them just for this request.
            config.headers = {"myHeader":"Special"}

            return config;
        }
    };
});

$httpProvider.interceptors.push('myHttpInterceptor');
Jonathan
  • 5,495
  • 4
  • 38
  • 53