Amazon API Gateway
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

启用基本请求验证的示例 API 的 OpenAPI 定义

以下 OpenAPI 定义定义了一个启用了请求验证的示例 API。该 API 是 PetStore API 的一部分。其使用 POST 方法将宠物添加到 pets 集合,并使用 GET 方法按指定类型查询宠物。

在 API 级别的 x-amazon-apigateway-request-validators 映射中声明了两种请求验证程序。params-only 验证程序在 API 上启用,并由 GET 方法继承。该验证程序让 API Gateway 可以验证所需的查询参数 (q1) 是否包含在传入请求内且不为空。all 验证程序在 POST 方法上启用。该验证程序验证所需的标头参数 (h1) 是否已设置且不为空。它还会验证负载格式是否符合指定的 RequestBodyModel 架构。这种模型要求输入 JSON 对象包含 nametypeprice 属性。name 属性可以是任何字符串,type 必须是一种指定枚举字段 (["dog", "cat", "fish"]),而 price 必须介于 25 和 500 之间。id 参数不是必需参数。

有关此 API 行为的更多信息,请参阅在 API Gateway 中启用请求验证

OpenAPI 2.0
OpenAPI 2.0
{ "swagger": "2.0", "info": { "title": "ReqValidators Sample", "version": "1.0.0" }, "schemes": [ "https" ], "basePath": "/v1", "produces": [ "application/json" ], "x-amazon-apigateway-request-validators" : { "all" : { "validateRequestBody" : true, "validateRequestParameters" : true }, "params-only" : { "validateRequestBody" : false, "validateRequestParameters" : true } }, "x-amazon-apigateway-request-validator" : "params-only", "paths": { "/validation": { "post": { "x-amazon-apigateway-request-validator" : "all", "parameters": [ { "in": "header", "name": "h1", "required": true }, { "in": "body", "name": "RequestBodyModel", "required": true, "schema": { "$ref": "#/definitions/RequestBodyModel" } } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "POST", "requestParameters": { "integration.request.header.custom_h1": "method.request.header.h1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } }, "get": { "parameters": [ { "name": "q1", "in": "query", "required": true } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "GET", "requestParameters": { "integration.request.querystring.type": "method.request.querystring.q1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } } } }, "definitions": { "RequestBodyModel": { "type": "object", "properties": { "id": { "type": "integer" }, "type": { "type": "string", "enum": ["dog", "cat", "fish"] }, "name": { "type": "string" }, "price": { "type": "number", "minimum": 25, "maximum": 500 } }, "required": ["type", "name", "price"] }, "Error": { "type": "object", "properties": { } } } }