Interviews2 answers
How to add HTTP headers using Interceptors?
2 Answers
Config :
app.config([ '$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('APIInterceptor');
} ]);
Service :
app.service('APIInterceptor', [function() {
var service = this;
service.request = function(config) {
config.headers.YOUR_HEADER= YOUR_HEADERS_VALUE;
return config;
};
}]);
Rajkiran SwainPosted Jun 7, 2017, 3:11 AM EDT
app.module('appName').factory('tokenInterceptor', ['$q', function($q) {
return {
request: function(config) {
config.headers = config.headers || {};
config.headers['X-Access-Token'] = 'xYRTT342aa@@55#$@';
config.headers['X-User-Key'] = '[email protected]'
config.headers['Content-Type'] = "application/json";
return config || $q.when(config);
},
response: function(res) {
return response || $q.when(res);
}
};
}]);
app.module('appName').config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('tokenInterceptor');
}]);
Angular will call interceptors when a request is done to the server.
Santosh Kumar AdidawarpuPosted Feb 10, 2017, 10:01 PM EST

