背景

我有一个使用Swagger 2.0定义和API Gateway extensions创建的API网关。

我覆盖了默认的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"
    }
}

但是,当我通过不带date属性的API网关控制台进行测试时:
Request body does not match model schema for content type application/json: [
  object has missing required properties (["date"])
]

并且带有无效的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 映射,但是到目前为止我还没有做到这一点。

最佳答案

更新:
现在可以通过网关响应来实现。使用以下模板设置 BAD_REQUEST_BODY 网关响应:

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

(API网关开发人员)
不幸的是,目前不支持此功能。我们正在积极致力于解决此问题,但是我无法在什么时候提供支持的具体时间表。

关于amazon-web-services - 从AWS API Gateway请求验证器获取详细的错误消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47953570/

10-13 09:15
查看更多