NodeJS Socket hang up when sending post form data in a loop
See original GitHub issueDescribe the bug
I have a list of users and I need to update their profile picture, I am doing a for loop to go through all the users and I am sending my form data request. The first one is always working, the second one depends on his humor and after that I got socket hang up error. The interesting thing is that the script not calling the remote server.
To Reproduce
- Have an API server with upload file
- Create a list of users
- Loop on this list
- Send Post request with form data
- Log the results
// Example code here
const picture = fs.createReadStream('pictures/logo.jpg')
for (let i = 0; i < nbUsers ; i += 1) {
const url = `ENDPOINT API`;
const form = new FormData();
let response;
form.append('0', picture);
form.append('action', 'upload');
const headers = form.getHeaders();
headers.Cookie = cookies;
try {
response = client.post(url, form, { headers: headers });
} catch (e) {
console.error(e.message);
}
return response;
}
Expected behavior
All requests are supposed to be send to the server.
Environment
- Axios Version 0.24.0
- Node.js Version 14.17.5
- OS: Ubuntu 18.04.6
Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Error: socket hang up in Http Request made in Node JS using ...
I am trying to make an Http Request using request-promise inside a for loop. But it seems if a Http Request takes long,...
Read more >How to handle a socket hang up error in Node.js usually - Quora
* The data sent is incorrectly formatted or unexpected, and the server chokes and closes the connection (server problem). Check for HTTP POST...
Read more >Socket hang up error - Help - Postman community
Making a get request returns a response. · Iterating over the response array till the length of the array · The test (test...
Read more >[Solved]-Socket.io sending lots of request on start up
The first POST with each sid is the connecting. The following GET is just polling, while the POST are pong packets, to say...
Read more >The Node.js Event Loop, Timers, and process.nextTick()
The event loop is what allows Node.js to perform non-blocking I/O ... data, etc. ... close callbacks: some close callbacks, e.g. socket.on('close', ...)...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes he’s right you need to create a stream for each request.
The problem was on my side I tried to reuse the stream of my file and for a reason it causes the socket hang up. If someone is interesting to explain my mistake I will enjoy 😃. For discovering it I rewrote my code with the another rest client and I had the same problem.