node.jsaxiosrequesthttp-status-code-504

Cannot post multiple and long requst Axios


I need to send post request to Another API, which will return result about 7 seconds.

If I try to send post one by one, it's working great. But as soon as I loop it or send multiple request, it returns errors.

The errors are sometimes Error: socket hang up or status code: 504

I've tried

It doesn't matter with writeFile(), I've deleted, it didn't work.

My guess is that since the first request takes about 7 to 8 seconds, that's why it causes error for next request?


to sum up

I need to post multiple requests that each of them takes about 7 to 8 seconds. But it returns socket hang up error or status code: 504 GATEWAY TIME OUT

what should I do?


Solution

  • Turns out my problem was not about axios, it's about form-data.

    the first request going well, but seconds, i don't know why but I think there are problem with form-data, maybe the form data is null something

    so I figured out to initialize formdata every time I send axios.post()

    here's the code

    const arr = [some arrya i need, like 4 elements];
    
    await Promise.all(arr.map(async(elem) => {
        const formData = new FormData();
        formData.append('image', createReadStream(pictureUri));
        const headers = { ... };
        ...
        const response = await axios.post(url, formData, {headers: headers});
        do some work...
    }))
    

    key is I had to generate form-data every time, even though it'll be always same...