question

salazarpresentys avatar image
salazarpresentys asked

How to manage a Rest Api Calling on Lambda Function using nodeJS

Hi,

I am developing an Alexa Skill using nodeJS 10 and lambda function as endpoint backend.

I am trying to call Rest Api (with GET method) on lambda function using nodeJS, but i am having difficulties because of execution time on lambda function. I have exported my lambda function with a deployment package in order to develop using VSCode and I have installed 'request' npm package (https://www.npmjs.com/package/request) and I am the code does the following:


the function which implements the calling.

function GetMedicamento(){
    var request = require('request');
    var response = request.get('https://cima.aemps.es/cima/rest/medicamento?cn=754614', {json: true }, (err, res, body) => {
        if (err) { return console.log(err); }
        console.log(body.url);
        console.log(body.explanation);
    });
    return response;
}


the function which playing the calling. _ I only have a console.log() with the api calling because I want to make sure the response is good.

const BuscaMedicamentoHandler = {
    canHandle(handlerInput){
        const request = handlerInput.requestEnvelope.request;
        return request.type === 'IntentRequest' && request.intent.name === 'BuscaMedicamentoIntent';
    },
    handle(handlerInput){
        console.log(GetMedicamento());
        var number = handlerInput.requestEnvelope.request.intent.slots.numero.value;
        speechOutput = "Código nacional: " + number;
        return handlerInput.responseBuilder
        .speak(speechOutput)
        .reprompt(speechOutput)
        .getResponse();
    }
};


The lambda function is playing the calling (I have the certainty because I can see that in the cloudwatch logs) but the response come back with a null body, so I cannot do nothing. I said 'execution time' because I think that the problem could be that I need to use an 'async'-'await' clauses or 'promises' but I don't have many experience developing in nodeJS.


could someone let me see an example of implementing that funtionality?

thanks in advance,

Pedro.



alexa skills kitalexalambdaapinode_js
10 |5000

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

1 Answer

Jason@Amazon avatar image
Jason@Amazon answered
1 comment
10 |5000

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

yes! thanks a lot, Jason!

0 Likes 0 ·