2

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;
        }
    };
}]);

1 Answer 1

4

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}
});
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but this way I have to add the 'X-WP-Nonce' headers in all the http requests. I am looking for a way to set the headers once in angular config. And change it when someone logs in.
Maybe $httpProvider.defaults.headers can help you so
Ok I'll try that in the future. I'll use your method for now. Many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.