【发布时间】:2019-10-13 05:40:10
【问题描述】:
我正在尝试编写一个简单的 AWS Lambda 函数来从外部公共 API 检索数据。我没有任何运气从互联网上复制和粘贴代码。
我已将代码精简为尽可能简单以使其保持简单。公共 API 为:https://swapi.co/api/people/1/
如何从公共 API 取回数据?
const https = require('https');
exports.handler = async (event) => {
var options = {
method: 'GET',
host: 'https://swapi.co/api/people/1/',
};
console.log('options', options);
const req = https.request(options, (res) => {
console.log('statusCode: ${res.statusCode}')
console.log(JSON.stringify(res))
});
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
AWS 编辑器中的日志文件显示:
START RequestId: 3ba3f23a-11c2-40af-b9e7-0258a6531728 Version: $LATEST
2019-05-27T16:17:44.839Z 3ba3f23a-11c2-40af-b9e7-0258a6531728 INFO options { method: 'GET', host: 'https://swapi.co/api/people/1/' }
END RequestId: 3ba3f23a-11c2-40af-b9e7-0258a6531728
REPORT RequestId: 3ba3f23a-11c2-40af-b9e7-0258a6531728 Duration: 305.90 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 26 MB
【问题讨论】:
标签: node.js aws-lambda