Skip to content
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

Duplicated URL params #280

Open
petrprikryl opened this issue Jun 25, 2024 · 5 comments
Open

Duplicated URL params #280

petrprikryl opened this issue Jun 25, 2024 · 5 comments

Comments

@petrprikryl
Copy link

axios.get('/api', {
  params: {
    foo: 'bar',
  },
});

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

@guomh
Copy link

guomh commented Jul 4, 2024

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

@zhuchanglin
Copy link

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
Copy link

apank commented Jul 12, 2024

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
Copy link
Contributor

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
Copy link

MechaGirls commented Jul 30, 2024

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants