I'm trying to write a Lambda function that uses the request-promise library to make an external https request. This is what I have:
exports.handler = async (event, context, callback) => {
console.log("Starting call: " + event.queryStringParameters.filter);
console.log("Decoded param: " + decodeURIComponent(event.queryStringParameters.filter));
var filter = JSON.parse(decodeURIComponent(event.queryStringParameters.filter));
console.log("Filter: " + filter);
var ingredients = filter.ingredients;
var options = {
uri: 'https://api.*****.com/search',
qs: {
app_id: '****',
app_key: '*****',
q: ingredients.join(' ')
},
json: true
};
console.log("Done calling stuff");
rp(options)
.then(function(recipes) {
console.log('Response: ' + recipes);
var recipesToReturn = [];
recipes.hits.forEach(function(recipeHit) {
recipesToReturn.push(objectMapper(recipeHit.recipe, recipeMap));
});
console.log('Recipes:', recipesToReturn);
const response = {
statusCode: 200,
body: JSON.stringify(recipesToReturn),
};
return JSON.stringify(response);
})
.catch(function(err) {
console.log('Error:', err)
const response = {
statusCode: 400,
body: err,
};
return JSON.stringify(response);
});
};
When I test out the API Gateway request, I see this:
Sun May 26 16:59:21 UTC 2019 : Execution failed due to configuration error: Malformed Lambda proxy response
I've been trying to read up on how the Lambda Proxy Responses should be formatted, and I'm assuming that I'm missing something with regards to the callback or the context, but I have not been able to figure out how to make it work with the request-promise library.