Amazon web services AWS模拟集成与动态状态代码
我使用一个swagger/OpenAPI文件来描述我在awsapigateway中实现的模拟API。当编写如下代码时,API返回HTTP 200:Amazon web services AWS模拟集成与动态状态代码,amazon-web-services,aws-api-gateway,swagger-2.0,Amazon Web Services,Aws Api Gateway,Swagger 2.0,我使用一个swagger/OpenAPI文件来描述我在awsapigateway中实现的模拟API。当编写如下代码时,API返回HTTP 200: "x-amazon-apigateway-request-validator": "params-only", "x-amazon-apigateway-integration": { "requestTemplates": { "application/json" : "{\"statusCode\": 200 }"
"x-amazon-apigateway-request-validator": "params-only",
"x-amazon-apigateway-integration": {
"requestTemplates": {
"application/json" : "{\"statusCode\": 200 }"
},
"responses" : {
"2\\d{2}" : {
"statusCode" : "200"
},
"4\\d{2}" : {
"statusCode" : "400"
}
},
"passthroughBehavior": "when_no_match",
"timeoutInMillis": "20000",
"httpMethod": "POST",
"type": "mock"
}
然而,当我改变swagger以返回在请求头中找到的值时,我得到一个HTTP500错误。以下是修改后的招摇:
"x-amazon-apigateway-request-validator": "params-only",
"x-amazon-apigateway-integration": {
"requestTemplates": {
"application/json" : "{\"statusCode\": 200 }"
},
"responses" : {
"2\\d{2}" : {
"statusCode" : "$input.params('Mock-Return-Code')"
},
"4\\d{2}" : {
"statusCode" : "$input.params('Mock-Return-Code')"
}
},
"passthroughBehavior": "when_no_match",
"timeoutInMillis": "20000",
"httpMethod": "POST",
"type": "mock"
}
下面是我在使用$input.params引用运行API时收到的确切错误:
由于配置错误,执行失败:输出映射引用了无效的方法响应:$input.params('Mock-Return-Code'))
有什么想法吗?我发现我走错了方向。我本应该调整请求模板,而不是调整响应模板。这里有一个例子,我得到的工作 首先,我必须更新路径参数,以便声明可选的头
"get": {
"parameters": [
{
"name": "x-mock-response-code",
"in": "header",
"required": false,
"type": "string",
"default": "200"
}
]
}
接下来,我修改了API网关集成,以使用头值来确定要返回的响应模板
"x-amazon-apigateway-request-validator": "params-only",
"x-amazon-apigateway-integration": {
"requestTemplates": {
"application/json": "#set($rc = $input.params('x-mock-response-code'))\r\n\r\n#if($rc.length() != 0)\r\n {\r\n \"statusCode\" : $rc\r\n }\r\n#else\r\n {\r\n \"statusCode\" : 200\r\n }\r\n#end"
},
"requestParameters" : {
"integration.request.header.x-mock-response-code" : "method.request.header.x-mock-response-code"
},
"responses" : {
"2\\d{2}" : {
"statusCode" : "200"
},
"4\\d{2}" : {
"statusCode" : "400"
}
},
"passthroughBehavior": "when_no_match",
"timeoutInMillis": "20000",
"httpMethod": "POST",
"type": "mock"
}
下面以漂亮的格式显示JSON请求模板代码:
#set($rc = $input.params('x-mock-response-code'))
#if($rc.length() != 0)
{
"statusCode" : $rc
}
#else
{
"statusCode" : 200
}
#end