使用 POSTMan 调用时 APIGateway 不执行请求验证

Sri*_*ram 4 amazon-web-services aws-api-gateway

刚刚通过 AWS 学习 - 我有一个带有 Lambda 代理集成的 APIGateway REST API 设置。API 定义了一个模型,并使用该模型在主体上请求验证设置。

说模型是

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "propertyA": {
            "type": "string"
        },
        "propertyB": {
            "type": "string"
        },
        "propertyC": {
            "type": "string"
        },
        "propertyD": {
            "type": "string"
        }
    },
    "required": ["propertyA", "propertyB", "propertyC", "propertyD"]
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我通过 APIGateway 控制台测试 API,并故意提供无效的输入(省略必需的属性propertyD):

{
    "propertyA": "valueA",
    "propertyB": "valueB",
    "propertyC": "valueC"
}
Run Code Online (Sandbox Code Playgroud)

请求失败并出现错误(400):Sun Jul 11 13:07:07 UTC 2021 : Request body does not match model schema for content type application/json: [object has missing required properties (["propertyD"])]

但是,当我使用来自 Postman 的相同无效输入调用相同的 API(和阶段)时,验证似乎没有发生,并且请求被代理到 Lambda,只要我注释掉部分代码,它甚至会返回 200 OK这取决于propertyD.

这里有什么区别?我应该从客户端传递任何请求标头吗?我在 AWS 文档中找不到任何内容

Sri*_*ram 8

回答我的问题-

问题在于请求中使用的标头 - Postman 默认 JSON 作为 a Content-Typeof text/plain,我必须使用 Body 选项卡中的下拉列表切换到 JSON,以使 PostMan 将 Content-Type 设置为 application/json

遵循这篇文章似乎已经解决了问题:https://itnext.io/how-to-validate-http-requests-before-they-reach-lambda-2fff68bfe93b,尽管它没有解释如何

显然,神奇之处在于在部分Content-Type下添加标题的配置HTTP Request Headers,即使标题设置正确,如application/jsonPostMan 中所示。