11

I have a webapp with multiple controllers. I'm setting the defaults headers of the $http service in a callback in one of my controllers (via http.defaults.headers.common['headername']). However, those headers do not get set in subsequent calls from other controllers. Do I have to set them for each controller I have or is one time enough?

7
  • have you tried using a $resource?, it's pretty odd what you mentioned, where are you setting this common header while bootstrapping the app? Commented Nov 25, 2014 at 19:39
  • I was trying to use the $http service because I just need to make a call, I don't need the full $resource service. I'm not setting the header while bootstrapping the app: I retrieve the value of the header from another server and then set it, but the application is alreay started. Commented Nov 25, 2014 at 20:19
  • You're right, using a $resource it works the way I'm trying to do it. However I don't know why the $http service does not work? Commented Nov 25, 2014 at 20:26
  • It does not depend on the $resource vs $http thing. I need to set the deafult headers in every controller I make calls from, and the $http service works just as fine. So the question would be: is there a way to set the headers globally at runtime so that I don't need to set the headers manually in every controller? Commented Nov 25, 2014 at 20:34
  • does the value change on every request? Commented Nov 25, 2014 at 20:45

1 Answer 1

15

You should use one of two methods:

Set $http.defaults.headers in run block e.g.

 module.run(function($http) {
  $http.defaults.headers.common.Authorization = 'Basic Token';
});

Use interceptor

var interceptor = function() {
  return {
    'request': function(config) {
      config.headers['Authorization'] = 'Basic Token';
     }
  }
};

angular.module('app', [])
  .config(function ($httpProvider) {
    $httpProvider.interceptors.push(interceptor);
});
Sign up to request clarification or add additional context in comments.

5 Comments

I cannot set the header in the run or config block, the value is still not available at that point.
Hm, interceptor works in that time when your code makes request. So all information is known in that moment. You can pass the value via service. Btw work with headers via controller is not good idea. Controllers should be thin and any business and API logic should be placed in services.
You mean I could inject a service in the interceptor which would set the value of the header only if the value was available, and that would work during runtime? For some reason I thought that all the initialization in a config block was "static", but I'll try this approach.
I succedeed in my objective using the interceptor solution, however I had to use a provider and reorganize the structure of my services because it leaded to a circular dependency. Thanks.
circular dependency is known problem with interceptors. I solved it by injector app.factory('postInterceptor', function(APP_CONF, $injector) { var auth = $injector.get('Auth'); if ( auth.access_token ) { config.headers['Authorization'] = 'Bearer ' + auth.access_token; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.