简体   繁体   中英

Node.js Lambda Function “response is invalid” Amazon Alexa

UPDATE: I had a mistake on my http request endpoint. I had not set the appropriate authentication options so that fixed a lot of errors possibly this specific one.

My question is similar to one here:

Node.js Lambda function returns "The response is invalid" back to Alexa Service Simulator from REST call

However the solution to that question does not solve my problem. So I make an http request call to an xsjs service in Hana cloud. I am getting the 'response is invalid' error message. I can't see why. Here is my function:

 // Create a web request and handle the response.
function httpGet(query, callback) {

    console.log("/n QUERY: "+ query);

    var host = 'datacloudyd070518trial.hanatrial.ondemand.com'; 
    var path = '/LocationInformation/getLocationInfo.xsjs?location='; 
    var hostname = 'https://' + host + path + query; 


    var auth = 'user1:D1anafer'; 

    var req = http.request({'hostname': hostname,
                            'auth': auth
                        }, (res) => {

    var body = '';

        res.on('data', (d) => {
            body += JSON.stringify(d);
        });

        res.on('end', function () {
            callback(body);
        }); 

    }); 


    req.end();

    req.on('error', (e) => {
        console.error(e);
    }); 


}

And the function that calls it:

'getNewsIntent': function () {

    //self = this; 

    httpGet(location, function (response) {

        // Parse the response into a JSON object ready to be formatted.
        //var output = JSON.parse(response); 
        //output = output['change']; 
        var output = response; 

        var cardTitle = location; 
        var cardContent = output; 

        alexa.emit(':tellWithCard', output, cardTitle, cardContent);

    }); 

}, 

Thank You -Diana

Inside your AWS account go to your Lambda function and click on the monitoring tab, where you should see "View Logs in Cloudwatch" in the right hand corner. If you click that link and you should see the errors that are being produced.

You can also use console.log() to log any information being returned from your REST api, which will be logged in cloudwatch and can help you see where your errors are.

This is just a guess from the top of my head. To really help some detailed error message would be required like mentioned about.

But just a guess: Your http.request() is using the http module ( https://nodejs.org/api/http.html ) and your are accessing the a https resource. If so there is a https ( https://nodejs.org/api/https.html ) module or use something like axios https://www.npmjs.com/package/axios or requestjs ( https://github.com/request/request ) this will handle both.

Like I said just a blind guess without detailed error message and seeing your require statements but I am happy to dive deeper if you happen to have details.

HTH

Your callback from the Lambda has to return a valid status code and body. Like this:

let payload = {
    statusCode: 400,
    body: JSON.stringify('body'),
    headers: {"Access-Control-Allow-Origin": "*"}
};
callback(null, payload);

On top of that, to call this from client side code, you have to pass the CORS header back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM