代码之家  ›  专栏  ›  技术社区  ›  Alix Axel

从AWS API网关请求验证程序获取详细错误消息

  •  34
  • Alix Axel  · 技术社区  · 8 年前

    出身背景

    我使用Swagger 2.0定义创建了一个API网关 API Gateway extensions .

    我覆盖了默认的API网关响应,例如:

    x-amazon-apigateway-gateway-responses:
      BAD_REQUEST_BODY:
        statusCode: 400
        responseTemplates:
          application/json: |
            {
              "error": {
                "code": 400,
                "stage": "$context.stage",
                "request": "$context.requestId",
                "message": "$context.error.message"
              }
            }
    

    这个 $context 上述有效载荷来自 API Gateway variables .

    我的API中的示例资源/方法如下所示(始终 LAMBDA_PROXY 集成):

    paths:
      /test:
        post:
          parameters:
            - in: body
              name: Test
              required: true
              schema:
              $ref: "#/definitions/Test"
          responses:
            201:
              description: Created
            400:
              description: Bad Request
            401:
              description: Unauthorized
            403:
              description: Forbidden
          x-amazon-apigateway-integration:
          uri: >-
            arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${lambda}/invocations
          type: aws_proxy
          httpMethod: POST
          credentials: "${credentials}"
          passthroughBehavior: never
    

    使用相应的请求有效负载定义:

    definitions:
      Test:
        type: object
        title: Test
        required:
          - date
        properties:
          date:
            type: string
            pattern: "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$"
            description: Date in YYYY-MM-DD Format
    

    以及 request validator extensions :

    x-amazon-apigateway-request-validator: body
    x-amazon-apigateway-request-validators:
      body:
        validateRequestBody: true
        validateRequestParameters: false
    

    问题

    当我调用此端点时,缺少或无效 date ,我总是得到同样的回答:

    {
        "error": {
            "code": 400,
            "stage": "latest",
            "request": "6b7a64f5-e7f0-11e7-845b-f53ceb4cb049",
            "message": "Invalid request body"
        }
    }
    

    但是,当我通过API网关控制台测试它时 日期 属性:

    Request body does not match model schema for content type application/json: [
      object has missing required properties (["date"])
    ]
    

    还有一个病人 日期 :

    Request body does not match model schema for content type application/json: [
      ECMA 262 regex "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$" does not match input string "2017/12/25"
    ]
    

    问题

    如何访问详细的错误消息,以便使用比 Invalid request body ? 我怀疑这一定是可能的,也许使用 x-amazon-apigateway-gateway-responses 映射,但到目前为止我还没能做到。

    5 回复  |  直到 8 年前
        1
  •  40
  •   tuomastik zoubir REMILA    5 年前

    更新:

    现在,这可以通过网关响应实现。设置a BAD_REQUEST_BODY 使用以下模板的网关响应:

    {"message": "$context.error.validationErrorString"}
    

    (API网关上的开发人员)

    遗憾的是,目前不支持此操作。我们正在积极努力解决这个问题,但我不能给你任何具体的时间表时,这可以得到支持。

        2
  •  6
  •   BMW    7 年前

    既然API网关开发人员已经回答了这个问题,我仍然想为您添加一些提示,也许这是有帮助的,这是一个可以接受的答案!

    对于您的问题,实际上您需要激活api网关的cloudwatch日志,这样,您可以获得比以前更多的日志。

    让我知道它是否包括 Request Validator

    aws document - How do I enable Amazon CloudWatch Logs for APIs I created in the Amazon API Gateway? 提供有关如何启用它的步骤。

    但我更喜欢这份文件 API Gateway and Lambda Logs ,这样屏幕截图就可以轻松地进行后续操作。

    在api网关中,您应该看到这已启用。 enter image description here

    多次访问API网关,查看日志组,其名称为:

    API-Gateway-Execution-Logs_{rest-api-id}/{stage_name}
    

    enter image description here

    这比你所掌握的信息更详细 Invalid request body 以及其他,例如 {"message": "Internal server error"} . 这是一个非常有用的特性,可以节省我很多时间来解决无服务器和api网关问题。

        3
  •  5
  •   BMW    7 年前

    在这种情况下,在网关响应部分中,转到:

    Bad Request Body [400]
    
    Change the value of the body mapping template to: 
    
    {"message":$context.error.validationErrorString}
    

    Ex输出:

    {
    "message": "[instance value (\"us\") not found in enum (possible values: [\"usd\",\"eur\",\"gbp\",\"jpy\",\"aud\",\"chf\",\"cad\",\"nzd\"])]"
    }
    
        4
  •  2
  •   Noel Llevares    8 年前

    这不是对您问题的回答,而是我们在应用程序中使用的一种替代解决方案,具有相同的用途(请求验证)。

    我们的无服务器API是从在API网关中定义所有端点开始的(包括Swagger文档)。随着时间的推移,我们添加了更多的端点(大约60多个端点,包括遗留REST端点、公共REST端点和私有graphQL端点)。

    事实证明,通过API网关管理如此多的端点非常繁琐,部署时间也很长(我们正在使用 serverless ).

    最终,我们决定将其缩减为三个“整体式”无服务器应用程序。两个REST端点和一个GraphQL端点。

    因此,基本上,我们在Lambda处理程序中处理路由(对于GraphQL来说,路由是不必要的)。

    对于请求验证,GraphQL是免费的(另一个喜欢GraphQL的原因)。至于我们的REST处理程序,我们使用JSON模式和任何验证错误,我们可以很容易地返回到客户端,并返回HTTP 400错误消息。

        5
  •  0
  •   AHonarmand    4 年前

    如果您在Cloudformation或SAM中执行此操作,请添加以下内容:

    SomeAPI:
      Type: 'AWS::Serverless::Api'
      Properties:
        GatewayResponses:
          BAD_REQUEST_BODY:
            ResponseTemplates:
              "application/json": '{ "message": "$context.error.validationErrorString"}'