Skip to content
Advertisement

Pass parameter/argument to axios interceptor

How do I send custom parameters to the axios interceptor? I am using an interceptor like this:

window.axios.interceptors.request.use(function (config) {
    if (PASSED_PARAM == true) {
        doSomethingAwesome();
    }

    return config;
}, function (error) {    
    return Promise.reject(error);
});

I also have a response interceptor that needs to receive the same parameter.

Advertisement

Answer

The method suggested by @Laurent will cause axios to wipe out all your other parameters and replace it with my_variable, which is may not exactly what you want.

The proper way of adding default parameters instead of replacing it is like this:

axios.defaults.params = {};
axios.interceptors.request.use(function (config) {
    config.params['blah-defaut-param'] = 'blah-blah-default-value';
    return config;
}, function (error) {
    return Promise.reject(error);
});

This works with axios 0.18.1. It does not work with axios 0.19 due to a regression bug…, I believe it will be fixed soon.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement