Sometimes, we want to retry original request and access original promise with Axios request interceptors.
In this article, we’ll look at how to retry original request and access original promise with Axios request interceptors.
How to retry original request and access original promise with Axios request interceptors?
To retry original request and access original promise with Axios request interceptors, we can call axios.request
with the error.config
object.
For instance, we write
axios.interceptors.response.use(
(response) => response,
async (error) => {
const status = error?.response?.status;
if (status === 401) {
await refreshToken(store);
error.config.headers[
"Authorization"
] = `Bearer ${store.state.auth.token}`;
error.config.baseURL = undefined;
return axios.request(error.config);
}
return Promise.reject(error);
}
);
to check if the response status is 401 with
status === 401
Then we call refreshToken
and then get the token from the store
and set it as the value of the Authorization
request header.
And then we call axios.request
with the error.config
and return the returned promise to retry the request.
Conclusion
To retry original request and access original promise with Axios request interceptors, we can call axios.request
with the error.config
object.