How to send an HTTP request using nodejs AWS Lambda?

I use AWS Lambda to manage the development of the Alexa Skill Kit. To track events, I would like the script to send an HTTP request at startup, however, from the cloud logs, it looks like the http.get function is skipped during the execution process.

The code is shown below (google.com replaces the analytics tracking URL that was tested in the browser);

 
exports.handler = function (event, context) {

    var skill = new WiseGuySkill();
    var http = require('http');

    var url = 'http://www.google.com';
    console.log('start request to ' + url)
    http.get(url, function(res) {
        console.log("Got response: " + res.statusCode);
        // context.succeed();
    }).on('error', function(e) {
        console.log("Got error: " + e.message);
        // context.done(null, 'FAILURE');
    });
    console.log('end request to ' + url);

    skill.execute(event, context);
};

Context objects were commented out to allow the "skill.execute" function to function, but in any case this HTTP request is not executed. Only "start" and "end" console.logs are written, internal functions do not work.

Is this an async problem? Thanks.

+4
2

HTTP-, http.get . , , HTTP- ( ), .

WiseGuySkill.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {

    // Call requestFunction to make the http.get call.
    // Get response from requestFunction using requestCallback
    requestFunction(function requestCallback(err) {

        // If error occurs during http.get request - respond with console.log
        if (err) {
            console.log('HTTP Error: request not sent');
        }

        ContinueIntent(session,response);
    });
};

"requestFunction" http.get .

function requestFunction(requestCallback){

        var url = "http://www.google.com";

        http.get(url, function(res) {
            console.log("Got response: " + res.statusCode);
            requestCallback(null);
        }).on('error', function (e) {
            console.log("Got error: ", e);
        });
    }

, , , "http" script. , - !

+1

, . :

  • API .
  • "", .

, ( customSkill, -). 200.

+1

Source: https://habr.com/ru/post/1614000/


All Articles