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 loop
aync/await
line by line like this
const 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?
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...