2
Reply

How to add HTTP headers using Interceptors?

Santosh Kumar Adidawarpu

Santosh Kumar Adidawarpu

Feb 11, 2017
1.3k
0

    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 Adidawarpu
    February 11, 2017
    1

    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 Swain
    June 07, 2017
    0