Raymond the Developer
Raymond the Developer

Reputation: 1676

Change http headers inside an angular controller

Hi I need to change http headers from inside a controller. It works in config by using $httpProvider but thats not what I am looking for.

I tried this but it says push of undefined.

$http.interceptors.push([function() {
    return {
        'request': function(config) {
            config.headers = config.headers || {};
            //add nonce to avoid CSRF issues
            config.headers['X-WP-Nonce'] = myLocalized.nonce;

            return config;
        }
    };
}]);

Upvotes: 2

Views: 121

Answers (1)

NotBad4U
NotBad4U

Reputation: 1542

In the config object of $http you have a field headers which is a map of strings or functions.

You can use it like that :

$http.get('/someUrl', { 
        headers: { Authorization : 'Basic ' + encoded}
});

Upvotes: 4

Related Questions