Interceptors allow you to run your code or modify the request/response before they are handled:
// Add a request interceptor
axios.interceptors.request.use(config => {
// Modify the request config
config.headers['Authorization'] = 'Bearer YOUR_TOKEN';
return config;
}, error => {
return Promise.reject(error);
});
// Add a response interceptor
axios.interceptors.response.use(response => {
// Modify the response data
return response.data;
}, error => {
return Promise.reject(error);
});
Leave a Reply