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?
-
have you tried using a $resource?, it's pretty odd what you mentioned, where are you setting this common header while bootstrapping the app?pedrommuller– pedrommuller2014-11-25 19:39:15 +00:00Commented 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.flower_green– flower_green2014-11-25 20:19:21 +00:00Commented 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?flower_green– flower_green2014-11-25 20:26:12 +00:00Commented 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?flower_green– flower_green2014-11-25 20:34:59 +00:00Commented Nov 25, 2014 at 20:34
-
does the value change on every request?pedrommuller– pedrommuller2014-11-25 20:45:16 +00:00Commented Nov 25, 2014 at 20:45
|
Show 2 more comments
1 Answer
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);
});
5 Comments
flower_green
I cannot set the header in the run or config block, the value is still not available at that point.
Gleb Vinnikov
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.
flower_green
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.
flower_green
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.
Gleb Vinnikov
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; } }