question

ultrajones avatar image
ultrajones asked

Lambda Function, Node.JS and HTTPS

Hello, I created a test Echo application using the Alexa Getting Started Guide and implemented it using an AWS Lambda function. The sample application is working great. However, I am struggling with trying to get Node.js to do a remote HTTPS connection to a remote web services. Does anyone have a simple example on how to use Node.js to connect to a remote web server that requires basic authentication? Regards, Ultrajones
alexa skills kitdebugging
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Kruse avatar image
Matt Kruse answered
Can you use the request module? https://github.com/request/request
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Anant avatar image
Anant answered
It's also possible to do using Node's native 'https' library. I found this snippet that communicates to the Twilio API over HTTPS in a Lambda-compatible node.js function. It includes Basic authentication as well: https://gist.github.com/stevebowman/7cff9dd80b227c899728 HTH!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

John Schettino avatar image
John Schettino answered
I use request, it works, but you need to upload it with your node app See https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ultrajones avatar image
ultrajones answered
I cannot get something as simple as this to work. Any ideas? var http = require('http'); var options = { hostname: ' www.amazon.com', method: 'GET', path: '/' }; var req = http.get(options, function(res){ console.log("statusCode: ", res.statusCode); // I am not seeing this in the logs console.log("headers: ", res.headers); res.on('data', function(data) { console.log("response data:"+data); }); res.on('end', function() { console.log("Done"); }); res.on('error', function(e) { console.log("Got error: " + e.message); }); }); Regards, Ultrajones Message was edited by: ultrajones
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

John Schettino avatar image
John Schettino answered
did you install the http module locally, and upload the zip of the directory?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Greg Crawford avatar image
Greg Crawford answered
As I noted in a different thread, you need to make sure that you are properly waiting for the external request to be made and also to wait for a response. https://forums.developer.amazon.com/forums/thread.jspa?threadID=5554&tstart=0
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Greg Crawford avatar image
Greg Crawford answered
The http and https modules seem to be available locally.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

John Schettino avatar image
John Schettino answered
> As I noted in a different thread, you need to make > sure that you are properly waiting for the external > request to be made and also to wait for a response. > > https://forums.developer.amazon.com/forums/thread.jspa > ?threadID=5554&tstart=0 Yeah, that's probably it - I missed that too - node is funny For those async calls, you need to put your completion callback to Alexa in the callback fn for the async call: So for example, using whatever you use to make the https call, this kinda thing is needed [pre] function getMode(intent, session, callback) { var cardTitle = "Get Smart Home Mode"; var sessionAttributes = {}; var shouldEndSession = false; var speechOutput = ""; // make the Async call, the function() callback has the completion callback to Alexa GetViaSmartThingsREST("mode", function (err, res, body) { // console.log("SUCCESS: ", err); var mode = "unknown" try { var jmodes = JSON.parse(body); mode = jmodes.mode; } catch (e) { console.log(e); console.log(body); } speechOutput = "The house is in " + mode + " mode right now."; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); }); } [/pre] Otherwise Node kills the async call, since nothing is waiting on it. Node. Full of fun! Message was edited by: John Schettino Message was edited by: John Schettino
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Greg Crawford avatar image
Greg Crawford answered
Node.js is fun stuff!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.