【问题标题】:Returning HTTP Status Code from Lambda via API Gateway通过 API Gateway 从 Lambda 返回 HTTP 状态码
【发布时间】:2016-01-27 20:23:14
【问题描述】:

我有一个带有 Python 处理程序的 Lambda 设置,我创建了一个 API 端点来仅接收 POST 方法。我想不通的是我的 Lambda 应该如何告诉 API Gateway 事情已经成功完成并返回 HTTP 200 状态代码。有人冒险走这条路吗?

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway


    【解决方案1】:

    在 python lambda 中只做

    raise Exception('notfound')
    

    使用任何其他关键字代替 notfound。

    然后在 apigateway 中,您需要将 'notfound' 映射到一些响应状态代码。我为此使用 swagger,所以这里是如何跟踪 'notfound' 的示例:

           "/tags/getById": {
              "get": {
                "summary": "Find the tag by it's ID",
                "produces": [
                  "application/json"
                ],
                "responses": {
                  "200": {
                    "description": "successful operation",
                    "schema": {
                      "$ref": "#/definitions/Tag"
                    }
                  },
                  "404": {
                    "description": "No tag found"
                  }
                },
                "parameters": [xxxxxxx],
                "x-amazon-apigateway-integration": {
                  "requestTemplates": {xxxxx},
                  "uri": {xxxxxx},
                  "responses": {
                    "default": {
                      "statusCode": "200"
                    },
                    "notfound": {
                      "statusCode": "404"
                    }
    
                  },
                  "httpMethod": "POST",
                  "type": "aws"
                }
              }
           }
    

    【讨论】:

      【解决方案2】:

      您应该能够通过使用处理程序的返回值来实现您的目标。见官方AWS Lambda Python Model Handler Types

      您还需要使用您的Integration Response 正确配置API Gateway。您可以在官方API Gateway Introductory Blog post 中找到有关如何完成此操作的更多信息,最重要的是Integration Response 部分。

      【讨论】:

        【解决方案3】:

        我也想要其他 http 错误代码,例如 400 和 500,所以我在这里写了一个答案。更多详情请查看:

        https://stackoverflow.com/a/41343990/984471

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-02-09
          • 2018-04-23
          • 2021-07-29
          • 2019-05-29
          • 2015-09-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多