AWS ApiGateway 自定义请求验证响应

Phi*_*ipp 5 validation amazon-web-services aws-api-gateway

我已将 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"
   ]
}
Run Code Online (Sandbox Code Playgroud)

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

{
  "entity": "vehicleType",
  "message": "missing"
}
Run Code Online (Sandbox Code Playgroud)

如果我查看网关的日志,似乎记录了类似的消息 ( 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
Run Code Online (Sandbox Code Playgroud)

这可以通过 API 网关实现吗?

tgk*_*tgk 5

是的,你想要的是$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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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


I B*_*jwa 4

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

{
  "message": "Invalid request body"
}
Run Code Online (Sandbox Code Playgroud)

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

从下面的链接查看更多详细信息,尤其是底部部分。

在 API Gateway 中测试基本请求验证

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

{
    "message":"$context.error.messageString", 
    "Hint":"Check required number of parameters or parameters allowed type and length."
}
Run Code Online (Sandbox Code Playgroud)

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

{
    "message": "Invalid request body",
    "Hint": "Check required number of parameters or parameters allowed type and length."
}
Run Code Online (Sandbox Code Playgroud)

请参阅热更新附加屏幕截图中的身体映射模板。(API 网关/网关响应)。 在此输入图像描述

  • 感谢你的回答!这可能会朝着正确的方向发展。我想包含无法验证的真实属性,而不是静态“提示”属性。如果您查看上面的 API 网关日志示例:我正在谈论属性“vehicleType”。但我想现在这是不可能的?! (2认同)