softonic / axios-retry

Axios plugin that intercepts failed requests and retries them whenever possible
Other
1.9k stars 167 forks source link

Duplicated URL params #280

Open petrprikryl opened 5 months ago

petrprikryl commented 5 months ago
axios.get('/api', {
  params: {
    foo: 'bar',
  },
});

First request will be: /api?foo=bar Second: /api?foo=bar&foo=bar ...

guomh commented 4 months ago

i have the same problem. i update the newest version. this still happened

zhuchanglin commented 4 months ago

I temporarily used the following method to avoid this issue. I'm not sure if there's a better way to handle it.

  instance.interceptors.request.use(conf => {
    const config: InternalAxiosRequestConfig = { ...conf };

    // set request id
    const requestId = nanoid();
    config.headers.set(REQUEST_ID_KEY, requestId);

    // retry bug fix
    clearDuplicatedQueryString(config);

    // config cancel token
    const cancelTokenSource = axios.CancelToken.source();
    config.cancelToken = cancelTokenSource.token;
    cancelTokenSourceMap.set(requestId, cancelTokenSource);

    // handle config by hook
    return opts.onRequest?.(config) || config;
  });

  // Clear duplicate query string parameters
  function clearDuplicatedQueryString(config: InternalAxiosRequestConfig) {
    const isAbsoluteURL = /^https?:\/\//i.test(config.url!);
    if (isAbsoluteURL) config.params = {};
  }
apank commented 4 months ago
instance.interceptors.request.use((config) => {
  console.log(config);
  return config;
});

On the first request it will return config.url with just the path -

But when I log the response from on onRetry

axiosRetry(instance, {
  retries: 3,
  retryCondition: (e) => isNetworkOrIdempotentRequestError(e),
  onRetry: (retryCount, error, requestConfig) => {
    console.log(requestConfig);
  },
});

requestConfig.url returns the absolute url; tried to track down if it's axios-retry or axios that is changing the url from a path to absolute but was unable to

obscurecat64 commented 4 months ago

can i ask, how are you guys forming the url like /api?foo=bar and /api?foo=bar&foo=bar?

config.url and config.baseURL return the server URL that will be used for the request (not including params) and config.params is an object (so it shouldn't have duplicated keys).

i can't seem to figure out 🙈

MechaGirls commented 3 months ago

在请求拦截器中添加以下代码,可以优雅的解决你的问题

instance.interceptors.request.use(config => {
    config.url = config.url.split('?')[0]
    return config;
  });