Request Interceptors in JavaScript

From the Axios cheat sheet · Interceptors & Error Handling · verified Jul 2026

Request Interceptors

Modify requests before they are sent

javascript
// Add request interceptor
axios.interceptors.request.use(
  (config) => {
    // Modify config before request is sent
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = \`Bearer \${token}\`;
    }
    console.log('Request:', config.method, config.url);
    return config;
  },
  (error) => {
    // Handle request error
    return Promise.reject(error);
  }
);
✅ Perfect for adding auth tokens to every request globally
💡 Multiple interceptors execute in the order they were added
🔍 Must return config object or Promise for request to proceed
⚡ Use eject() to remove interceptor if no longer needed
interceptorsrequestmiddlewareauth

Continue with Axios

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full Axios cheat sheet