Zahidur Rahman
Zahidur Rahman

Reputation: 1718

Node JS: Call external HTTP request by 'https' library

When i call external http request by basic 'https' library , i cannot mention port address ,but when i show log error it shows port address is '80' why it shows and where is the problem in my code??

    var http = require("http");
    http.createServer(function (request, response) {
    var options = {
      hostname: 'example.com',
      method: 'GET',
      headers: {
                'Authorization':'Bearer 22bc5ce9f7934da6616ac7d883dbb8c9',
                'Content-Type': 'application/json'
      }
    };

    var req = http.request(options, (res) => {
      res.setEncoding('utf8');
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
      res.on('end', () => {
        console.log('No more data in response.');
      });
    });
    req.end();
    req.on('error', (e) => {
      console.log(e);
    });


    // Listen on the 8080 port.
    }).listen(8080);

Error Message:

{ Error: getaddrinfo ENOTFOUND https://example.com:80
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:79:26)
  code: 'ENOTFOUND',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'https://example.com',
  host: 'https://example.com',
  port: 80 }

Upvotes: 0

Views: 512

Answers (1)

govgo
govgo

Reputation: 635

"getaddrinfo ENOTFOUND means client was not able to connect to the given address. Please try specifying host without http." Try this:

var options = {
      host: 'example',
      method: 'GET',
      headers: {
                'Authorization':'Bearer 22bc5ce9f7934da6616ac7d883dbb8c9',
                'Content-Type': 'application/json'
      }
};

See this post Here

Upvotes: 1

Related Questions