23

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?

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Dmitriy
  • 529
  • 2
  • 9
  • 24
  • I also have found sometimes that if I crawl a site too aggressively (like 10+ simultaneous connections) they'll start responding with socket hang-ups, so it could be that too. – tobek Sep 15 '14 at 23:03

7 Answers7

36

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

req.end()
user568109
  • 43,824
  • 15
  • 87
  • 118
  • 3
    I've added this line (please, look at the updated question), but still get the error. – Dmitriy Sep 18 '13 at 09:22
  • @Dmitriy Are you using the same code somewhere else,is there more to your code. You have to end every request. Be sure to run the posted code separately. – user568109 Sep 18 '13 at 12:15
18

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.

Community
  • 1
  • 1
Jonathan Lonowski
  • 112,514
  • 31
  • 189
  • 193
  • 1
    I've added this line (please, look at the updated question), but still get the error. Also I try to use http.get() method. I take an example from node.js documentation, but still get the "socket hang up" error. – Dmitriy Sep 18 '13 at 09:22
  • 2
    @Dmitriy Hmm. Adding `http = require 'http'` at the top, I'm able to see the response from your current snippet. You might check DNS to ensure the computer can reach the server -- `require('dns').lookup('www.ya.ru', console.log);` – Jonathan Lonowski Sep 18 '13 at 10:13
  • 1
    dns.lookup works normally for me. I get the following callback arguments: error: null, address: '77.88.21.3', family: 4. May be some problems is that I use a proxy server for the internet connection? – Dmitriy Sep 18 '13 at 10:42
  • Thank you this is CORRECT! – CommaToast Oct 07 '14 at 01:44
10

in my case it was the 'Content-Length' header - I took it out and it's fine now...

code:

function sendRequest(data)
{
    var options = {
              hostname: host,
              path: reqPath,
              port: port,
              method: method,
              headers: {
                      'Content-Length': '100'
              }
    var req = http.request(options, callback);
    req.end();
    };

after removing the line: 'Content-Length': '100' it sorted out.

Aviram Netanel
  • 10,273
  • 7
  • 36
  • 63
5

I've finally detected the problem and found out the solution. The problem was that I use a proxy server to connect to the internet. Here is the working code:

options = 
    hostname: 'myproxy.ru'
    path: 'http://www.ya.ru'
    port: 3128
    headers: {
        Host: "www.ya.ru"
    }
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()

Thank you all for helps and suggestions!

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Dmitriy
  • 529
  • 2
  • 9
  • 24
1

I found this to occur in one more case where I was sending empty body like - '{}' in a delete operation called from intern framework for testing; instead I used null to send as value of body parameter while making the request through

Rajat Talwar
  • 11,876
  • 1
  • 16
  • 12
0

When upgrading from 0.10.33 to 0.12 of nodejs, this error was hit.

In my case, there was a body (json) for the delete request. Earlier, node client was setting - 'transfer-encoding' as chunked - by default when 'content-length' is not set. It seems in recent version - node client stopped setting transfer-encoding by default.

Fix was to set it in the request.

Sushil
  • 4,356
  • 2
  • 15
  • 15
0

In my case, after upgrading to node 8.0.0, the post does not work anymore. adding Content-Length to header does not help. Have to add 'Connection': 'keep-alive' to the header instead to get this error away.

    let postOptions = {
        method: 'POST',
        form: form,
        url: finalUrl,
        followRedirect: false,
        headers:{
            'Connection': 'keep-alive'
        }
    };
    request(postOptions, handleSAMLResponse);
LeOn - Han Li
  • 6,655
  • 52
  • 49