gpt4 book ai didi

aws-lambda - AWS API Gateway Serverless 验证请求正文并返回错误消息作为响应

转载 作者:行者123 更新时间:2023-12-03 16:31:36 24 4
gpt4 key购买 nike

我正在使用 Serverless 设置 AWS API 网关.理想情况下,不应使用 AWS 控制台配置任何内容,仅使用 Serverless 进行部署。

我想让它验证进入也在这个项目中设置的各种 Lambda 函数的请求正文。此验证应通过将收到的正文与关联的 JSON 架构文件进行比较来完成。在格式不正确的请求的情况下,我希望响应指示缺少或错误键入的属性。

我找到了 serverless-reqvalidator-plugin解决针对模式进行验证的问题。验证项在 Resource 中描述,架构与使用 serverless-aws-documentation plugin 的自定义部分中的验证项相关联。 ,然后最后使用 http 事件中的 reqValidatorName 属性将函数与函数部分中的验证对象相关联。理想情况下,在这里的某个地方,我想设置在出现错误时应在响应中返回什么消息。

// serverless.yml

resources:
Resources:
BodyValidation:
Type: "AWS::ApiGateway::RequestValidator"
Properties:
Name: 'BodyValidation'
RestApiId:
Ref: ApiGatewayRestApi
ValidateRequestBody: true
ValidateRequestParameters: false
/// Ideally, some property about how to dynamically return an error message for invalid requests would be here, or bellow

custom:
documentation:
// --- cut out api info
models:
- name: BodyValidationRequest
contentType: "application/json"
schema: ${file(./SampleValidationRequest.json)}

functions:
SampleValidationFunction:
//--- cut handler info
events:
- http:
//--- cut path and method info
reqValidatorName: BodyValidation
documentation: // --- cut documentation info



我按照 this answer 的指示这样做了对于类似的问题。我有兴趣在不依赖 reqvalidator 插件的情况下执行此操作,但是在 this example 中AWS API Gateway 文档中给出,它没有展示如何使用无服务器来做到这一点。

看起来像这样 wasn't possible in the past ,但似乎 be possible now .不过,我找不到任何严格使用 serverless.yml 的示例。

我正在 AWS 控制台和 Postman 中测试请求。例如,这是我正在测试的模式:
// SampleValidationRequest.json

{
"type": "object",
"properties" :{
"name": {
"type": "string"
},
"id": {
"type": "number"
}
},
"required": ["name", "id"]
}

当发送缺少参数的主体时,例如 id 与:
{
"name": "test"
}

我在响应正文中收到回复
{
"message": "Invalid request body"
}

我希望它说的是我在查看 AWS 中的日志时看到的内容
{
"message": "Request body does not match model schema for content type application/json: [object has missing required properties (["id"])]
}

所以,总结一下:

1. 是否可以添加一些属性来动态解释响应消息中的请求正文有什么问题?

2. 是否可以仅使用来自无服务器的 AWS API 设置而不是使用 reqvalidator 来针对架构验证请求正文?

最佳答案

  1. Is it possible to add some property that dynamically explains what is wrong with the request body in the response message?


在一定程度上。请参阅此线程: https://github.com/serverless/serverless/issues/3896

您需要添加 resource归因于 serverless.yml像这样:
resources:
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: ${self:provider.stage}-${self:service}
GatewayResponseResourceNotFound:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
RestApiId:
Ref: 'ApiGatewayRestApi'
ResponseType: BAD_REQUEST_BODY
"StatusCode" : "422"
ResponseTemplates:
application/json: "{\"message\": \"$context.error.message\", \"error\": \"$context.error.validationErrorString\"}"

  1. Is it possible to validate a request body against a schema just using the setup from Serverless for the AWS API, instead of using reqvalidator?


对的,这是可能的。确保更新到最新版本的无服务器,否则它将无法生成 API 网关模型。

这是一个对我有用的设置:
    events:
- http:
path: my_method/
method: post
request:
schema:
application/json: ${file(validate_my_method.json)}

关于aws-lambda - AWS API Gateway Serverless 验证请求正文并返回错误消息作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55799137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com