I have been trying without success to use http module in my Node.js endpoint to do a simple http get.

I have followed the various tutorials to execute the get within my intent, but it keeps failing with getaddrinfo ENOTFOUND in the cloudwatch log.

It seems like I am preparing the url correctly, if I just cut and past the url output into the browswer I get the expected response, and its just a plain http get over port 80.

I suspect that maybe the Alexa hosted lambda doesn't have permission necessary to make remote calls to non-amazon web services, but I don't know this for sure.

Can anybody shed any light? FYI this is the code in my lambda:

var http = require('http');

function httpGet(address, zip, zillowid) {
const pathval = 'www.zillow.com/webservice/GetSearchResults.htm' + `?zws-id=${zillowid}` + `&address=${encodeURIComponent(address)}&citystatezip=${zip}`;
console.log ("pathval =" + pathval);
return new Promise(((resolve, reject) => {
   var options = {
    host: pathval,
    port: 80,
    method: 'GET',
};

const request = http.request(options, (response) => {
  response.setEncoding('utf8');
  console.log("options are" + options);
  let returnData = '';

  response.on('data', (chunk) => {
    returnData += chunk;
  });

  response.on('end', () => {
    resolve(JSON.parse(returnData));
  });

  response.on('error', (error) => {
      console.log("I see there was an error, which is " + error);
    reject(error);
  });
});
request.end();
}));
}

1 Answers

0
Michael - sqlbot On Best Solutions

host: pathval is incorrect usage of the Node.js http module. You need to provide the hostname and the path + query string as two different options.

An example of correct usage:

host: 'example.com',
path: '/webservice/GetSearchResults.htm?zws-id=...',

(Of course, these can be variables, they don't need to be literals as shown here for clarity.)

The error occurs because you're treating the whole URL as a hostname, and as such it doesn't exist.

I suspect that maybe the Alexa hosted lambda doesn't have permission necessary to make remote calls to non-amazon web services

There is no restriction on what services you can contact from a within a Lambda function (other than filters that protect against sending spam email directly to random mail servers).