0

I have written a node.js Lambda function for my skill and I am having trouble with the return data from a http request call. My endpoint for this request is an SAP hana application that returns a JSON file. I have tested this separately, fixed some authentication problems that I was having. I finally made sure this worked separately.

Now, I have the following httpGet function:

function httpGet(query, callback) { 

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

//var path = '/LocationInformation_2/getLocationInformation_2.xsjs?location=';
//var url = 'https://' + host + path + query; 

var options = {
    //http://api.nytimes.com/svc/search/v2/articlesearch.json?q=seattle&sort=newest&api-key=
    host: host, 
    path: '/LocationInformation_2/getLocationInformation_2.xsjs?location=' + query, 
    method: 'GET', 
    //headers: { 'Content-Type': 'application/json' }
    json: true
};

var req = http.request(options, (res)  => {

    var body = '';
    console.log('status code: ' + res.statusCode); 

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

    res.on('end', function () {
        console.log('body: ' + body); 
        callback(body);
    });

});

req.end();

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

}

And this function that calls it:

    'getNewsIntent': function () {


    httpGet(location, function(response) {

        var output = response['change']; 


        var cardTitle = location; 
        var cardContent = output; 

        alexa.emit(':tellWithCard', output, cardTitle, cardContent);
        // alexa.emit(':tell', 'Hi'); 
    });

}, 

The problem now, is, that whenever I call on this getNewsIntent(), there is absolutely no text on the output. As suggested by @craig_h, I looked at my CloudWatch to maybe get some information. And I am getting the following messages.

enter image description here

So in getNews() I have the line 'console.log('body: ' + body)'. I do not see this body here. I also get a 302 Status Code. What could this mean?

Diana Vazquez Romo
  • 152
  • 1
  • 1
  • 11

1 Answers1

0

So the answer was simply to deal with HTTP get redirection. So instead of using the http.request(options, function(res) { }) I downloaded the Node js request module, found here; https://www.npmjs.com/package/request

which does handle redirection events. By 'redirection events' I mean things like, your http endpoint only works with 'http' vs. 'https' and little details like that.

So my function ended up looking like so:

function Request(query, callback) {

var url = 'https://' + host + path + query; 


request(url, function(error, response, body) { 

    console.log('error: ', error); 
    console.log('statusCode: ', response && response.statusCode); 
    console.log('body: ', body); 
    callback(body); 

}); 

}

Credits to this stack overflow gem :

How do you follow an HTTP Redirect in Node.js?

And support from here:

https://people.sap.com/thomas.jung

Diana Vazquez Romo
  • 152
  • 1
  • 1
  • 11