Axios 拦截器重试发送 FormData
Axios interceptor to retry sending FormData
我正在尝试为包含 FormData
作为数据的请求创建“重试”功能。
重试发生在 JWT 令牌过期时,我的第二个请求(重试请求)不包含任何数据。
想法:
const axiosInstance = axios.create();
/**
* Before each request, we'll add a possible retry if the
* request need a refreshed token
*/
axiosInstance.interceptors.request.use((request) => {
if (typeof _.get(request, ['retried']) === 'undefined') {
request.retried = false;
}
return request;
});
/**
* After a request with an error, we'll check if the error is a token not refreshed.
* If we didn't already tried to refresh, we'll just fallback to the errorHandler function
*/
axiosInstance.interceptors.response.use(null, async (error) => {
const statusCode = _.get(error, ['response', 'data', 'statusCode']);
const message = _.get(error, ['response', 'data', 'message']);
const { config: request = {} } = error;
if (statusCode === 401 && !request.retried) {
try {
const newTokens = await refreshTokens();
request.retried = true;
_.set(request, ['headers', 'authorization'], newTokens.accessToken);
return axiosInstance.request(request);
} catch (e) {
return error;
}
}
return error;
});
要求:
const formData = new FormData();
formData.append('name', name);
formData.append('file', fs.createReadStream(file.path));
axiosInstance.post(
'http://localhost/upload',
formData,
{
headers: {
...formData.getHeaders(),
authorization: tokens.accessToken
}
}
);
如果我的令牌过期,请求将失败,然后拦截器将刷新我的令牌,并使用新的 header Authorization
重试完全相同的请求。然而,在服务器端,接收到的负载始终为空。
据我所知,重试时需要重新添加formData(我认为是由于数据被添加为流并在请求期间被消耗,所以新的重试不能消耗已经消耗的流)。
你可以看看request.data
明白我的意思。
您可以在配置参数中传递表单数据:
axiosInstance.post(
'http://localhost/upload',
{},
{
data: formData, // <---- pass it here
headers: {
...formData.getHeaders(),
authorization: tokens.accessToken
}
}
);
我正在尝试为包含 FormData
作为数据的请求创建“重试”功能。
重试发生在 JWT 令牌过期时,我的第二个请求(重试请求)不包含任何数据。
想法:
const axiosInstance = axios.create();
/**
* Before each request, we'll add a possible retry if the
* request need a refreshed token
*/
axiosInstance.interceptors.request.use((request) => {
if (typeof _.get(request, ['retried']) === 'undefined') {
request.retried = false;
}
return request;
});
/**
* After a request with an error, we'll check if the error is a token not refreshed.
* If we didn't already tried to refresh, we'll just fallback to the errorHandler function
*/
axiosInstance.interceptors.response.use(null, async (error) => {
const statusCode = _.get(error, ['response', 'data', 'statusCode']);
const message = _.get(error, ['response', 'data', 'message']);
const { config: request = {} } = error;
if (statusCode === 401 && !request.retried) {
try {
const newTokens = await refreshTokens();
request.retried = true;
_.set(request, ['headers', 'authorization'], newTokens.accessToken);
return axiosInstance.request(request);
} catch (e) {
return error;
}
}
return error;
});
要求:
const formData = new FormData();
formData.append('name', name);
formData.append('file', fs.createReadStream(file.path));
axiosInstance.post(
'http://localhost/upload',
formData,
{
headers: {
...formData.getHeaders(),
authorization: tokens.accessToken
}
}
);
如果我的令牌过期,请求将失败,然后拦截器将刷新我的令牌,并使用新的 header Authorization
重试完全相同的请求。然而,在服务器端,接收到的负载始终为空。
据我所知,重试时需要重新添加formData(我认为是由于数据被添加为流并在请求期间被消耗,所以新的重试不能消耗已经消耗的流)。
你可以看看request.data
明白我的意思。
您可以在配置参数中传递表单数据:
axiosInstance.post(
'http://localhost/upload',
{},
{
data: formData, // <---- pass it here
headers: {
...formData.getHeaders(),
authorization: tokens.accessToken
}
}
);