'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
multiple
then().then().th
async/await
inside for loopaync/await
line by line like thisconst toonifyNewFileName = `./uploads/Toonified-toonify-${baseFileName}` const toonifyRes = await axios.post(toonifyUrl, formData, {headers: headers}) var {data:{num_faces, b64_encoded_output}} = toonifyRes; var base64Buffer = Buffer.from(b64_encoded_output, "base64"); writeFile(toonifyNewFileName, base64Buffer, err => console.log("DONE 1")); const comicNewFileName = `./uploads/Toonified-comic-${baseFileName}` const comicRes = await axios.post(comicUrl, formData, {headers: headers}) var {data:{num_faces, b64_encoded_output: newB64}} = comicRes; var base64Buffer2 = Buffer.from(newB64, "base64"); writeFile(comicNewFileName, base64Buffer2, err => console.log("DONE 2"));
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 1:[1]
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...
Solution 2:[2]
Obviously the socket is hanging! Don't bother with http, it is a little complex. Use node unirest and it closes the data stream. With this, you can loop and send files in batches remotely.
var unirest = require('unirest');
var req = unirest('POST', 'localhost:3200/store/artifact/metamodel')
.attach('file', '/home/arsene/DB.ecore')
.field('description', 'We are trying to save the metamodel')
.field('project', '6256d72a81c4b80ccfc1768b')
.end(function (res) {
if (res.error) throw new Error(res.error);
console.log(res.raw_body);
});
Hope this helps!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sandy_yeseul |
Solution 2 | Arsene Online |