user3762991
user3762991

Reputation: 1721

Add custom header values

I am new to angularjs. Currently learning about interceptors.

What I require is, I want to add a new header parameter like 'isUserUpdated' : true in the header so that I can identify at server side if the user updated.

My code is as follows:

.factory('myInterceptor', [function(){
            var myInterceptor = {
                request: function(config){
                    if(config.url.indexOf('/session') > -1) {
                        //add a new value in header
                    }
                    return config;
                },
            };
            return myInterceptor;
        }]);

Is there any way that I can add a new custom key value pair in the header?

Upvotes: 0

Views: 50

Answers (1)

Saurabh Sarathe
Saurabh Sarathe

Reputation: 231

You need to create an http Interceptor for this.

var app = angular.module('app'); // get your app.
app.config([ '$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('MyCustomInterceptor');
} ]);

app.service('MyCustomInterceptor', [ '$rootScope', function($rootScope) {
    var service = this;
    service.request = function(config) {
        config.headers.isUserUpdated = $rootScope.myValue; // true or false or whatever 
        return config;
    };
} ]);

config.headers.isUserUpdated will be added in each http call done from the application.

Upvotes: 1

Related Questions