$http Auth Headers in AngularJS

You’re mixing the use cases; instantiated services ($http) cannot be used in the config phase, while providers won’t work in run blocks. From the module docs:

  • Configuration blocks – […] Only providers and constants can be injected
    into configuration blocks. This is to prevent accidental instantiation
    of services before they have been fully configured.
  • Run blocks – […] Only instances and constants can be injected into run
    blocks. This is to prevent further system configuration during
    application run time.

So use either of the following:

app.run(['$http', function($http) {
    $http.defaults.headers.common['Authorization'] = /* ... */;
}]);
app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common['Authorization'] = /* ... */;
}])

Leave a Comment