How to add HTTP headers using Interceptors?
Santosh Kumar Adidawarpu
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.
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;}; }]);