Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *