3

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

4

3 回答 3

1

在 python lambda 中只是做

raise Exception('notfound')

而不是 notfound 使用任何其他关键字。

然后在 apigateway 中,您需要映射'notfound'到一些响应状态代码。我为此使用招摇,所以这里是如何跟踪的示例'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"
            }
          }
       }
于 2017-01-19T18:56:08.010 回答
0

您应该能够通过使用处理程序的返回值来实现您的目标。请参阅官方AWS Lambda Python 模型处理程序类型

您还需要API Gateway正确配置您的Integration Response. 您可以在官方API Gateway 介绍性博客文章中找到有关如何完成此操作的更多信息,最重要的是该Integration Response部分。

于 2015-10-30T02:05:32.360 回答
0

我也想要其他 http 错误代码,如 400 和 500,所以我在这里写了一个答案。请参阅它以获取更多详细信息:

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

于 2016-12-27T11:43:34.143 回答