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?
To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults. headers.
$http is an AngularJS service for reading data from remote servers.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With