Axios 请求失败,状态码为 429,但它正在使用 Postman但它、状态、Axios、Postman

2023-09-07 11:10:28 作者:向来缘浅奈何情深

我正在尝试使用 axios 访问此 API,但出现错误状态:429 [太多请求].我只发送一个请求,但仍然出现错误.

I am trying to access this API using axios but I am getting error with status: 429 [ Too many Requests ]. I am sending only one requests still getting an error.

但是当我尝试使用邮递员访问此网址时,它正在工作.

BUT when I try to access this url using postman it is working.

axios
  .post(
    `https://www.expedia.com/Hotel-Search-Data?responsive=true&destination=New+York%2C+New+York&latLong=40.75668%2C-73.98647&regionId=178293&startDate=01%2F20%2F2019&endDate=01%2F21%2F2019&rooms=1&adults=2&timezoneOffset=19800000&langid=1033&hsrIdentifier=HSR&page=7`
  )
  .then(result => {
    console.log(result.data);
  })
  .catch(err => {
    console.log(err);
  });

推荐答案

我遇到了同样的问题,并且由于请求太多而发生这种情况.

I ran into this same issue and this happens because of too many requests.

您可能在某个循环中运行上述代码行,这会导致这种情况发生,因为 Axios 正在同时触发所有请求.

You are probably running the above line inside some loop which is causing this to happen as Axios is firing off all requests simultaneously.

解决方法是等待响应,像这样

The workaround would be to await the response, something like this

try {
  const result = await axios.post(`YOUR_URL`, {<Your JSON payload>});
} catch (error) {
  console.error(error);
}

请注意,以上代码必须在 async 函数中.

Please note the above code has to be inside an async function.