How to pass parameter or argument to Axios interceptor with JavaScript?

Sometimes, we want to pass parameter or argument to Axios interceptor with JavaScript.

In this article, we’ll look at how to pass parameter or argument to Axios interceptor with JavaScript.

How to pass parameter or argument to Axios interceptor with JavaScript?

To pass parameter or argument to Axios interceptor with JavaScript, we can add it to the config.params object.

For instance, we write:

axios.interceptors.request.use((config) => {
  config.params = {
    ...config.params,
    myVar: 'value'
  }
  return config
})

We add the myVar property to config.params by spreading its existing properties into a new object and then add myVar after it.

Conclusion

To pass parameter or argument to Axios interceptor with JavaScript, we can add it to the config.params object.