Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Socket hang up" error during request

Tags:

I try to make a GET request to some site (not mine own site) via http module of node.js version 0.8.14. Here is my code (CoffeeScript):

options =          host: 'www.ya.ru'         method: 'GET'     req = http.request options, (res) ->         output = ''         console.log 'STATUS: ' + res.statusCode         res.on 'data', (chunk) ->             console.log 'A new chunk: ', chunk             output += chunk          res.on 'end', () ->             console.log output             console.log 'End GET Request'      req.on 'error', (err) ->         console.log 'Error: ', err     req.end() 

I get the following error during this operation: { [Error: socket hang up] code: 'ECONNRESET' }. If I comment the error handler my application is finished with the following error:

events.js:48     throw arguments[1]; // Unhandled 'error' event     ^ Error: socket hang up     at createHangUpError (http.js:1091:15)     at Socket.onend (http.js:1154:27)     at TCP.onread (net.js:363:26) 

I try to find out solution on the internet but still hasn't found them. How to solve this issue?

like image 459
Dmitriy Avatar asked Sep 18 '13 08:09

Dmitriy


People also ask

What causes a socket hang up?

When a socket hang up is thrown, one of two things happens: When you're a customer, When you send a request to a distant server as a client and don't get a response in a timely manner. This error is caused by the end of your socket.

What is socket hangup error in Postman?

Socket Hang up, where the server closed the connection for some reason. An example why this may occur is an incorrectly input client certificate or participant identifier preventing the message to reach the server. ECONNRESET, where an endpoint may be resetting the connection for some reason.


2 Answers

You have to end the request. Add this at the end of your script:

req.end() 
like image 102
user568109 Avatar answered Sep 19 '22 09:09

user568109


When using http.request(), you have to at some point call request.end().

req = http.request options, (res) ->     # ...  req.on 'error', # ...  req.end() # <--- 

Until then, the request is left open to allow for writing a body. And, the error is because the server will eventually consider the connection to have timed out and will close it.

Alternatively, you can also use http.get() with GET requests, which will call .end() automatically since GET requests aren't normally expected to have a body.

like image 37
Jonathan Lonowski Avatar answered Sep 19 '22 09:09

Jonathan Lonowski