简体   繁体   English

axios post 请求挂在有效请求上

[英]axios post request hangs on valid requests

I'm using axios in my nodejs app to send requests to another service.我在我的 nodejs 应用程序中使用 axios 将请求发送到另一个服务。
When I send a request and get 5xx or 4xx error codes in response - axios handles the request well and returns the error to the client.当我发送请求并得到 5xx 或 4xx 错误代码作为响应时 - axios 会很好地处理请求并将错误返回给客户端。
When the request is well structured, it just hangs and the promise axios sends never resolves, leaving the client hanging indefinitely waiting for a response.当请求结构良好时,它只是挂起并且 axios 发送的承诺永远不会解析,让客户端无限期地挂起等待响应。
I've no idea what's causing only valid requests to hang, or how to further debug this.我不知道是什么导致只有有效请求挂起,也不知道如何进一步调试。
Things I've tried so far -到目前为止我尝试过的事情 -

  • Send the requests (both failed and valid) using postman - they return error codes and OK codes as expected.使用邮递员发送请求(失败和有效) - 它们按预期返回错误代码和 OK 代码。
  • Debug the node process in VScode, I can see that axios is internally looping indefinitely, waiting for the promise to resolve but I've yet to understand what's keeping the promise from resolving.在VScode中调试节点进程,我可以看到axios在内部无限循环,等待promise解决,但我还没有理解是什么阻止promise解决。
  • axios version is a bit outdated so I've tried updating it but this created new issues so I decided to dial it back to the original version. axios 版本有点过时,所以我尝试更新它,但这产生了新问题,所以我决定将其拨回原始版本。

Some information on the versions of the app, in case relevant -如果相关,有关应用程序版本的一些信息 -

  • node version - v9.11.1节点版本 - v9.11.1
  • axios verion - 0.9.1 axios 版本 - 0.9.1

Snippet of axios request, redacted for security reasons - axios 请求的片段,出于安全原因进行了编辑 -

 const Router = require('express').Router; const router = Router(); const axios = require('axios').create({ baseURL: 'https://some-service.com' }); const auth-handler = require('auth-handler'); router.post('/', (req, res) => { auth-handler.getToken('token-name') .then(token => { const body = { subject: "subject", body: "body" }; const options = { headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' } }; return axios.post('/'+req.body.route+'/endpoint?foo=bar', body, options); }) .then(response => res.status(response.status).send(response.data)) .catch(error => { console.error(error); res.status(error.status).send(error.data); }); });

Faced similar issue.面临类似的问题。 Axios would get stuck in instance(request) step. Axios 会卡在实例(请求)步骤中。 Replacing https with http in URL fixed the issue.在 URL 中用http替换https解决了这个问题。

The answer to this is that axios has been resolving the promises as expected, there was just no indication for it on the client side of my program, only errors were indicated properly in the form.对此的答案是 axios 一直在按预期解决承诺,只是在我的程序的客户端没有任何指示,只有错误在表单中正确指示。

I've added an indication for 2xx responses, and now all is well again.我已经添加了 2xx 响应的指示,现在一切又好了。

Fo me it worked after using https://www.npmjs.com/package/axios-request-throttle对我来说,它在使用https://www.npmjs.com/package/axios-request-throttle后起作用了

static myFynction(urls): void {
    axiosThrottle.use(axios, {
        requestsPerSecond: 2
    });
    const getUrls = [];
    urls.forEach((url: string) => {
    const axiosCall = axios({
            method: 'GET',
            headers: {
                ...
            },
            url: url,
        })
        .then((response) => {
            return response.status;
        })
        .catch((error) => {
            return error.message;
        });
    getUrls.push(axiosCall);
});
const responses = browser.call(async () => await Promise.all(getUrls));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM