【问题标题】:AWS ApiGateway customize Request Validation ResponseAWS ApiGateway 自定义请求验证响应
【发布时间】:2018-02-01 13:16:16
【问题描述】:

我已将我的 AWS APIGateway 配置为根据 JSON 架构验证请求。

例如路径 /vehicle,附有以下架构:

{
   "type":"object",
   "properties":{
      "licensePlate":{
         "pattern":"[A-Za-z]{1,3} [A-Za-z]{1,2} \\d{1,4}",
         "type":"string"
      },
      "vehicleType":{
         "type":"string",
         "enum":[
            "Truck",
            "Trailer"
         ]
      }
   },
   "required":[
      "licensePlate",
      "vehicleType"
   ]
}

这很好用。如果我提交无效请求,API 会以 400 {"message": "Invalid request body"} 响应。我想自定义此消息,例如到

{
  "entity": "vehicleType",
  "message": "missing"
}

如果我查看来自网关的日志,似乎记录了类似的消息 (object has missing required properties (["vehicleType"]))。我可以用那个吗?如何访问它?

日志:

Execution log for request test-request
Thu Feb 01 13:12:18 UTC 2018 : Starting execution for request: test-invoke-request
Thu Feb 01 13:12:18 UTC 2018 : HTTP Method: POST, Resource Path: /vehicle
Thu Feb 01 13:12:18 UTC 2018 : Method request path: {}
Thu Feb 01 13:12:18 UTC 2018 : Method request query string: {}
Thu Feb 01 13:12:18 UTC 2018 : Method request headers: {}
Thu Feb 01 13:12:18 UTC 2018 : Method request body before transformations: {
    "licensePlate": "HH AB 123"
}
Thu Feb 01 13:12:18 UTC 2018 : Request body does not match model schema for content type application/json: [object has missing required properties (["vehicleType"])] 
Thu Feb 01 13:12:18 UTC 2018 : Method completed with status: 400

API 网关可以做到这一点吗?

【问题讨论】:

  • 您找到获取这些消息的方法了吗?

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


【解决方案1】:

是的,你想要的是$context.error.validationErrorString

就像@Bajwa 所说——您需要自定义 GatewayReponse 模板。如果您正在使用如下所示的云形成:

"GatewayResponse": {
  "Type": "AWS::ApiGateway::GatewayResponse",
  "Properties": {
    "ResponseParameters": {
      "gatewayresponse.header.Access-Control-Allow-Origin": "'*'",
      "gatewayresponse.header.Access-Control-Allow-Headers": "'*'"
    },
    "ResponseTemplates": {
      "application/json": "{\"error\":{\"message\":\"$context.error.messageString\",\"errors\":\"$context.error.validationErrorString\"}"
    },
    "ResponseType": "BAD_REQUEST_BODY",
    "RestApiId": {
      "Ref": "YouRestApiResource"
    },
    "StatusCode": "400"
  }
}

如果您违反了请求正文验证器,您将看到如下内容:

{
  "error": {
    "message":" "Invalid request body"",
    "errors":"[string \"1\" is too short (length: 1, required minimum: 10)]"
}

这并不完美——有些消息是模糊的,如果有人知道如何添加导致违规的属性名称,那就太好了。

【讨论】:

    【解决方案2】:

    如果请求有效负载无效,您将看到相同的消息:

    {
      "message": "Invalid request body"
    }
    

    API Gateway 包含更多详细信息,如果请求负载格式有效且参数格式无效。

    从下面的链接查看更多细节,尤其是底部。

    Test Basic Request Validation in API Gateway

    但是您可以通过自定义网关响应在响应消息中进行一些自定义。 您可以自定义Bad Request Body 400

    {
        "message":"$context.error.messageString", 
        "Hint":"Check required number of parameters or parameters allowed type and length."
    }
    

    然后您将看到以下格式的消息。

    {
        "message": "Invalid request body",
        "Hint": "Check required number of parameters or parameters allowed type and length."
    }
    

    从附加的屏幕截图中查看热门以更新身体映射模板。 (APIGateway/网关响应)。

    【讨论】:

    • 感谢您的回答!这可能会朝着正确的方向发展。而不是静态的hint 属性,我想包含无法验证的不动产。如果您从上面查看我的 API 网关日志示例:我说的是属性 vehicleType。但我想这现在不可能?!
    • 我们可以添加未能验证的不动产吗?即使我也面临同样的问题..
    猜你喜欢
    • 2017-05-30
    • 2021-05-14
    • 2020-09-15
    • 2021-06-19
    • 1970-01-01
    • 2018-05-31
    • 2016-07-22
    • 2017-11-17
    • 2020-07-28
    相关资源
    最近更新 更多