简体   繁体   中英

axios post request hangs on valid requests

I'm using axios in my nodejs app to send requests to another service.
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.
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.
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.
  • 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.
  • 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.

Some information on the versions of the app, in case relevant -

  • node version - v9.11.1
  • axios verion - 0.9.1

Snippet of axios request, redacted for security reasons -

 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. Replacing https with http in URL fixed the issue.

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.

I've added an indication for 2xx responses, and now all is well again.

Fo me it worked after using 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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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