-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to add a retry method to axios? #934
Comments
You can do that with an interceptor that handles authentication errors. Something like: axios.interceptors.response.use(null, (error) => {
if (error.config && error.response && error.response.status === 401) {
return updateToken().then((token) => {
error.config.headers.xxxx <= set the token
return axios.request(config);
});
}
return Promise.reject(error);
}); |
When using it I get "config is undefined". Where should I get it from? |
@mmsmsy This line:
should be:
|
Be careful if you're using the For example, the code from @rubennorte would crash with this setup: // Lets use qs to stringify our request.
this.$axios = axios.create({
transformRequest: [
data => qs.stringify(data),
],
});
this.$axios.interceptors.response.use(null, (error) => {
if (error.config && error.response && error.response.status === 401) {
return updateToken().then((token) => {
error.config.headers.xxxx <= set the token
// Here, the request data will be double stringified with qs.stringify,
// potentially leading to 422 responses or similar.
return this.$axios.request(config);
});
}
return Promise.reject(error);
}); A solution to this problem is not to transform the request data if it's already a string. For example: this.$axios = axios.create({
transformRequest: [
data => (isString(data) ? data : qs.stringify(data)),
],
}); |
@skyrpex Thanks a lot! Your answer brought us on the right track on a long and complicated bug hunt! It seems the default transformRequest handles transformation like in your example, but axios notices the data is a string and will set the content type to This was a hard bug to catch! |
remhume can you give me a hand? I'm having an issue with the same wrong behaviour that you had on your program. |
iirc we just manually set the content type to the correct one (in our case |
Thanks for the answer man I've already found other solution to solve my
issue. Regards
…On Sat, Jul 28, 2018 at 6:19 PM, remmy hume ***@***.***> wrote:
iirc we just manually set the content type to the correct one (in our case
application/json), which solved it.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#934 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AVqsr8akXZIWUoLgfSmk7X9f2FwLirA2ks5uLOONgaJpZM4Nu1Oc>
.
|
If i have 10 parralel requests, axios send 10 updateToken requests. P.S find solution here https://gist.github.com/mkjiau/650013a99c341c9f23ca00ccb213db1c |
@akhrabrov I made a simple package if you'd like to use it -> axios-auth-refresh. I'd be more than happy to see the contributions if you're missing some of the functionalities. |
I wanted to do something similar, except not necessarily with authentication, and to keep retrying after a delay when a particular status code is received. Essentially polling via an Axios interceptor. This was my solution:
https://gist.github.com/edmondburnett/38ed3451de659dc43fa3f24befc0073b |
You should also use below code for prevent crash in post requests : |
In my interceptors, I wish to catch a specific error (say, token expired)
And then I'll run some method to refresh the token.
After that resolves, I wish axios to retry the previous request (with the same result handler).
Do I need to implement this logic manually? Or is there a retry or something similar that allows me to do it directly?
Thanks
The text was updated successfully, but these errors were encountered: