Socket hung error in request module in nodejs even though CURL does the right thing

I am using an API with a noticeable timeout for the response and I always ran into Error: socket hang up

on the request

module in nodejs

, it did happen, although I could write the CURL url without any problem. I tried to increase the parameters timeout

in the query, but nothing good happened. Is there something else instead of a timeout? Should I use other packages instead request

?

code nodejs

:

var request = require("request");

var options = { 
  method: 'GET',
  url: 'https://someapiurl.com/api/search',
  qs: { q: 'Vanak sq' },
  timeout: 12000,
  time: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);
  console.log(body);
});

      

mistake:

Error: Error: socket hang up
    at Request._callback (/home/mohammad/Desktop/test.js:14:20)
    at self.callback (/home/mohammad/node_modules/request/request.js:188:22)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Request.onRequestError (/home/mohammad/node_modules/request/request.js:884:8)
    at emitOne (events.js:101:20)
    at ClientRequest.emit (events.js:188:7)
    at TLSSocket.socketOnEnd (_http_client.js:345:9)
    at emitNone (events.js:91:20)
    at TLSSocket.emit (events.js:185:7)

      

+3


source to share





All Articles