Axios interceptors allow us to intercept and modify requests or responses before they are handled by then or catch. Interceptors are commonly used for the following purposes:
- Modify request data before sending it to the server.
- Attach authentication information (e.g., JWT token) to the request headers before sending the request.
- Cancel requests before they reach the server.
- Handle all response errors uniformly.
- Transform response data before it reaches the application logic.
Using Axios interceptors primarily involves two types: request interceptors and response interceptors.
Adding Request Interceptors
Request interceptors are executed before the request is actually sent. Here is a general method to add a request interceptor:
javascript// Add request interceptor axios.interceptors.request.use(function (config) { // Do something before sending the request, e.g., set token config.headers.Authorization = `Bearer ${YOUR_AUTH_TOKEN}`; return config; }, function (error) { // Handle request errors return Promise.reject(error); });
Here, we first add a request interceptor using axios.interceptors.request.use. This interceptor receives two functions as parameters. The first function is called before the request is sent and receives the request configuration object config as a parameter, allowing us to modify this configuration. In the example above, we add an Authorization header with a hypothetical authentication token YOUR_AUTH_TOKEN. The second function is executed when a request error occurs; here we simply return the error.
Adding Response Interceptors
Response interceptors are called before the server's response data reaches then or catch. Here is a general method to add a response interceptor:
javascript// Add response interceptor axios.interceptors.response.use(function (response) { // Do something with the response data // You can process the response data as needed; this is just an example if (response.data && response.data.success) { return response.data.data; } else { // Handle unsuccessful cases here, or reject with an error return Promise.reject('Error with request'); } }, function (error) { // Handle response errors if (error.response.status === 401) { // If status code is 401, you can handle re-authentication, etc. } return Promise.reject(error); });
In this example, we add a response interceptor using axios.interceptors.response.use. It also receives two functions. The first function is called when a successful response is returned and receives the response object response as a parameter. In this function, we perform some simple checks and return only the necessary data part. The second function is called when a response error occurs, for example, you can handle 401 Unauthorized status codes by implementing automatic re-authentication or redirecting to the login page.
Removing Interceptors
If you want to remove an interceptor at some point, you can do the following:
javascript// Adding an interceptor returns an interceptor ID const myInterceptor = axios.interceptors.request.use(function () {/*...*/}); // Use this ID to remove the interceptor axios.interceptors.request.eject(myInterceptor);
In the above code, we first add a request interceptor and save the returned interceptor ID in the myInterceptor variable. Then, we call the axios.interceptors.request.eject method and pass this ID to remove the interceptor.