0

I am developing a simple nodejs application. I am trying to make a REST API call from that application an the REST API has basic authentication. I am getting error "ERRORError: getaddrinfo ENOTFOUND" Tried with the options suggested in stackoverflow but couldn't make it work. Can anyone please help me on this..Here is the code I am using `

var http = require('http');

var url = 'someurl';
var username = 'username';
var password = 'password';
var optionsget = {
  host: 'someurl',
  method: 'GET',
  auth: username + ':' + password,
  headers: {
     'Accept': 'application/json',
     'Content-Type': 'application/json'
  }
};

console.log('Do the GET call');

// do the GET request
var reqGet = http.get(optionsget, function(res) {
    console.log("Hii");
    console.log("statusCode: ", res.statusCode);

    res.on('data', function(d) {
        console.info('GET result:\n');
        process.stdout.write(d);
        console.info('\n\nCall completed');
    });

});

reqGet.end();
reqGet.on('error', function(e) {
    console.error("ERROR" +e);
});

Thanks in advance
Regards
VHC

1 Answers1

0

www.google.com is not a valid URL. The browser lets you type it (which tends to make people think it should work in all circumstances), but that's only because under the covers, the browser guesses that you meant to type http://www.google.com.

The http module requires a fully qualified URL such as http://www.google.com or it requires you to separately specify host and path and protocol in the options object. But, if you're just going to pass a URL, then it needs to be a full URL. Details in the node.js doc.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @user1948228 - Did this answer your question? If it did, then you can indicate that to the community by clicking the green checkmark to the left of the answer. That will also earn your some reputation points for following the proper procedure here on stack overflow. – jfriend00 Mar 24 '17 at 04:40