“Socket hang up” error during request

后端 未结 7 707
北海茫月
北海茫月 2020-12-16 11: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 = 
        ho         


        
相关标签:
7条回答
  • 2020-12-16 11:28

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

    req.end()
    
    0 讨论(0)
  • 2020-12-16 11:29

    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

    0 讨论(0)
  • 2020-12-16 11:29

    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);
    
    0 讨论(0)
  • 2020-12-16 11:38

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-16 11:44

    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.

    0 讨论(0)
提交回复
热议问题