Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Error: getaddrinfo ENOTFOUND" error when making an HTTPs request

here's the code in AWS Lambda function:

var https = require('https');
exports.handler = (event, context, callback) => {
    var params = {
        host: "bittrex.com",
        path: "/api/v1.1/public/getmarketsummaries"
    };
    var req = https.request(params, function(res) {
        var test = res.toString();
        console.log(JSON.parse(test));
        //console.log(JSON.parse(res.toString()));
    });
    req.end();
};

Error: getaddrinfo ENOTFOUND https://bittrex.com https://bittrex.com:443 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

Other solutions did not work.

like image 446
AmazingDayToday Avatar asked Oct 21 '17 20:10

AmazingDayToday


People also ask

How do I fix Getaddrinfo Enotfound error?

The getaddrinfo error can be solved by passing the entire URL instead of the options to the http. get() function. You can kick away the getaddrinfo error by adding the missing localhost line in the hosts file while using localhost.

What does error Getaddrinfo Enotfound mean?

If you try to run your JSON server and see this error message “getaddrinfo ENOTFOUND localhost,” it's happening because Webpack cannot find your localhost address.


2 Answers

Remove the https:// from the host. The require already says you're using https/SSL.

like image 124
stdunbar Avatar answered Sep 19 '22 18:09

stdunbar


I modified your code to work correctly in AWS Lambda Node.js 6.10. I set the Lambda timeout to be 60 seconds for testing.

The big change is adding "res.on('data', function(chunk) {}:" and "res.on('end', function() {}".

var https = require('https');
exports.handler = (event, context, callback) => {
    var params = {
        host: "bittrex.com",
        path: "/api/v1.1/public/getmarketsummaries"
    };
    var req = https.request(params, function(res) {
        let data = '';
        console.log('STATUS: ' + res.statusCode);
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            data += chunk;
        });
        res.on('end', function() {
            console.log("DONE");
            console.log(JSON.parse(data));
        });
    });
    req.end();
};
like image 22
John Hanley Avatar answered Sep 17 '22 18:09

John Hanley