【问题标题】:API Gateway new (beta) http apiAPI Gateway 新(测试版)http api
【发布时间】:2020-04-22 08:35:16
【问题描述】:

我使用 API 网关创建了一个示例 HTTP API(目前处于测试版中)。此 API 不使用任何身份验证,并且具有 lambda 作为集成。该路由接受任何 HTTP 方法,并且我已确认 lambda 具有适当的 API 网关权限。此权限是在我创建 API 时添加的。

但是,当我调用 API 时,我收到 HTTP 状态 500 和正文:{"message":"Internal Server Error"}.

如果我将其设置为 REST API 而不是 HTTP API,则相同的 lambda 和 API 将起作用。

任何想法为什么这在 H​​TTP API 中不起作用?

【问题讨论】:

    标签: amazon-web-services rest api http gateway


    【解决方案1】:

    Internal Server Error 也有类似的问题。我正在使用带有 nodejs lambda 集成的新 API Gateway HTTP API(测试版)。正如您所指出的,lambda 本身可以正常工作,而 REST API 也可以正常工作。

    花费我 8 小时的 2 个问题:

    1. 我必须在处理程序中添加 async 前缀或使用回调,但我不能只返回没有 async 的响应。见https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

    2. 响应结构必须正确,请参阅此页面:https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-create-lambda-backend,滚动到示例功能代码,您将看到:

        // The output from a Lambda proxy integration must be 
        // in the following JSON object. The 'headers' property 
        // is for custom response headers in addition to standard 
        // ones. The 'body' property  must be a JSON string. For 
        // base64-encoded payload, you must also set the 'isBase64Encoded'
        // property to 'true'.
    

    所以我的函数最终看起来像这样,注意async 和正确的响应结构:

    exports.handler = async function(event, context) {
      const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!')
      };
      return response;
    }
    

    【讨论】:

    • 艾哈迈德哈提卜 - 谢谢!这在新的 http api 中运行良好。
    猜你喜欢
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2020-11-26
    相关资源
    最近更新 更多