question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NodeJS Socket hang up when sending post form data in a loop

See original GitHub issue

Describe 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

  1. Have an API server with upload file
  2. Create a list of users
  3. Loop on this list
  4. Send Post request with form data
  5. 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:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
edandwebcommented, Mar 10, 2022

Yes he’s right you need to create a stream for each request.

1reaction
edandwebcommented, Nov 8, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found