aacerox / node-rest-client

REST API client from node.js
MIT License
377 stars 132 forks source link

"Error: socket hang up" when using node-rest-client if follow-redirects Ver > 1.13.3 #216

Open eddiebateson opened 2 years ago

eddiebateson commented 2 years ago

Hello

I've run in to an issue with an intermittent failure "Error: socket hang up" when performing an http GET call. The problem seems to be related to the version of follow-redirects, which is a dependency of node-rest-client.

Here is a sample bit of code calling a public endpoint api (so will be replicable for anyone): I ran this script using node in the powershell => node test.js

test.js

const Client = require('node-rest-client').Client; 

let endpoint = "https://apipheny.io/free-api/"; 

let query = `${endpoint}`; 
let args = __configArguments('text/plain'); 

for (let i = 0; i < 50; i++) { 
    try { 
        let req = (new Client()).get(query, args, (data) => { 
            console.log('test\n') 
            console.log(data) 
        });

        req.on('error', (err) => { 
            console.log('test1\n') 
            console.log('Error', err) 
        }); 
    } catch (error) { 
        console.log('test2\n') 
        console.log('Exception', error) 
    } 
} 

function __configArguments(contentType) { 
    return { 
        headers: { 
            'Content-Type': contentType, 
        }, 

        requestConfig: { 
            'timeout': 1000, 
            'noDelay': true, 
            'keepAlive': true, 
            'keepAliveDelay': 1000 
        }, 

        responseConfig: { 
            'timeout': 1000 
        } 
    }; 
}

an excerpt of the output is like this

test1

Error Error: socket hang up
    at connResetException (internal/errors.js:628:14)
    at TLSSocket.socketCloseListener (_http_client.js:449:25)
    at TLSSocket.emit (events.js:412:35)
    at net.js:675:12
    at TCP.done (_tls_wrap.js:563:7) {
  code: 'ECONNRESET',
  request: ClientRequest {
    _events: [Object: null prototype] { error: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    href: 'https://apipheny.io/free-api/',
    options: {
      host: 'apipheny.io',
      port: null,
      path: '/free-api/',
      href: 'https://apipheny.io/free-api/',
      method: 'GET',
      headers: [Object]
    },
    _httpRequest: Writable {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _options: [Object],
      _ended: true,
      _ending: true,
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 0,
      _requestBodyBuffers: [],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [ClientRequest],
      _currentUrl: 'https://apipheny.io/free-api/',
      _timeout: null,
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false
  }
}

As I mentioned earlier this seems to be caused by the version of follow-redirects, a dependency of node-rest-client. If I manually install follow-redirects version 1.13.3, we don't get this "Error Error: socket hang up". Anything later than that we start to see this issue.

Would anyone have an inkling to what is going on here, or any potential solutions?

Thank you kindly.

Vishwanath1688 commented 1 year ago

Request timeout is very less, increase to 5000 'timeout': 5000,