javascript - How to get a correct response from an API on AWS lambda
问题描述
This is my first time soliciting the Stack-Overflow community. Since a few days I have been learning to use the AWS lambda service connected with GETEWAY. I need to do a GET on an API but the problem is that I constantly receive a empty response.
Here is an example of my code with a free access API:
var getApi= async function(event) {
var x = await axios.get(url)
}
var getResponse = async function(){
var data= await getApi()
if (data.status ==200){
return data
}
}
exports.handler = async function() {
return getResponse().then(res => {
const response = {
statusCode: 200,
body: JSON.stringify(res),
};
return response
}).catch(error => { return error})
};
Thank you very much for your help,
解决方案
我自己最近遇到了这个问题。解决方案是:
- 如果您在 AWS 网关中使用 Lambda 作为授权方,则 Lambda 应返回包含 principalId、policyDocument 和 context 的 JSON 对象。
- 上下文是一个地图,您可以在其中添加自己的自定义变量,例如字符串、数字和布尔值。
JSON 对象的全部内容将返回到网关。查看此文档:https ://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
我还有一篇关于如何通过 Cloudformation YAML 文件配置网关的非常详细的 Stackoverflow 帖子:AWS API Gateway with Lambda Authorizer
推荐阅读
- python - 无法将 PySpark 数据帧转换为行数 > 15M 记录的 Pandas 数据帧
- typescript - 是否可以使用 ConstructorParameters 递归?
- r - 将 Markdown 文本存储为对象
- wpf - 绑定到另一个控件时,ControlTemplate DataTrigger 不更新
- python - 带有记录的 cookie 或自定义 cookie 的 Python Selenium GET
- python - 如何在视频剪辑中保存以前的帧?
- memory - OCaml 中的映射与折叠内存使用情况
- python - Whitespace causes problem in BigQuery with Python
- java - 运行集成测试时忽略 gRPC 全局拦截器
- c++ - C++ 如何从控制台中删除一行?