Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs httprequest with data - getting error getaddrinfo ENOENT

Update - Answered by self

I see one has to make sure that the DNS is resolved properly from the machine, check out the node documentation to make sure that domain is resolvable.

Original Question

i am writing a nodes based program,in which the user can ask me to do a httprequest on their behalf {off course they provide me with some data, and method to call with} but every time i do a httprequest it gives me an error

getaddrinfo ENOENT this is how my code looks

function makehttprequest(deviceid, httpaction, httppath,methods, actiondata, callback) { console.log('we are here with httpaction' + httpaction + ' path ' + httppath + ' method ' + methods + ' action data ' + actiondata);  //do the http post work, get the data, and call the callback function with return data  var options = {    host: httpaction,    port: 80,    path: httppath,    method: methods  };      try {       var req = http.request(options, function(res) {         console.log('STATUS: ' + res.statusCode);         console.log('HEADERS: ' + JSON.stringify(res.headers));         res.setEncoding('utf8');         res.on('data', function (chunk) {           console.log('BODY: ' + chunk);         });       });     } catch(e) {       console.log('error as : ' + e.message);     }      req.on('error', function(e) {       console.log('problem with request: ' + e.message);     });      // write data to request body     console.log('writing data to request ..');     req.write(actiondata);     console.log('finished writing data to request…');     req.end();     console.log('request ended…'); } 
like image 631
Brij Raj Singh - MSFT Avatar asked Mar 06 '12 08:03

Brij Raj Singh - MSFT


People also ask

How do you fix Getaddrinfo Enotfound error?

The error getaddrinfo ENOTFOUND localhost is caused by Webpack cannot found localhost address. To solve it, open the terminal: sudo nano /etc/hosts. Add following into the hosts file and save it.

What does error Getaddrinfo Enotfound mean?

getaddrinfo ENOTFOUND means client was not able to connect to given address. Please try specifying host without http: var optionsget = { host : 'localhost', port : 3010, path : '/quote/random', // the rest of the url with parameters if needed method : 'GET' // do GET };

What does Enotfound mean?

ENOTFOUND The ENOTFOUND exception occurs in Node.js when a connection cannot be established to some host due to a DNS error. This usually occurs due to an incorrect host value, or when localhost is not mapped correctly to 127.0.0.1 .


2 Answers

I've seen this happen when your host (which you pass in as httpaction) has the scheme (so "http://") in front of it. Your host should strictly be the domain like "www.google.com" not "http://www.google.com" or "www.google.com/hello-world" or "http://www.google.com/hello-world".

Keep it just the domain.

Here's an example: http://allampersandall.blogspot.com/2012/03/nodejs-http-request-example.html

like image 51
Micah Avatar answered Sep 21 '22 12:09

Micah


The problem can also happen if you have a trailing slash:

Good: "www.google.com"

Bad: "www.google.com/"

like image 31
rockerston Avatar answered Sep 20 '22 12:09

rockerston