Search code examples
node.jsamazon-web-servicesaws-lambdaalexa-skills-kit

HTTP request failing in AWS Lambda function for Alexa


I am trying to return data from an external api in my inline Lambda function but when I test this in the developer console for Alexa, I get 'There was a problem with the requested skills response' and I can't work out why. Also, as I am doing this from the AWS console, I can't console.log to see what it actually being returned.
(I have removed the default intents for the sake of the post)

const request = require('request');

const handlers = {
'LaunchRequest': function () {
    this.emit(':ask', 'Welcome');
},
'GiveUpdateIntent': function (){
    var slot = this.event.request.intent.slots.line.value;

    httpGet(slot, (theResult) => {
            this.response.speak(theResult);
            this.emit(':responseReady');
        });

}

};
function httpGet(query, callback) {
var options = {
    host: 'api.tfl.gov.uk',
    path: '/line/' + encodeURIComponent(query) + '/status',
    method: 'GET',
};

var req = http.request(options, res => {
    res.setEncoding('utf8');
    var responseString = "";

    //accept incoming data asynchronously
    res.on('data', chunk => {
        responseString += chunk;
    });

    //return the data when streaming is complete
    res.on('end', () => {
        console.log(responseString[0]);
        callback(responseString[0]);
    });

});
req.end();
}


exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

Solution

  • The issue turned out to be with using http itself instead of https.

    The only response back that I was getting was a status code of 302 which is a redirection because the api I was calling changes all http requests to https.

    Therefore, I changed my import to https and used the https.get method (instead of http.get) to call the api and the correct response was returned.