如何在 cloudformation 中为 api 网关自定义响应
How to customize reponse in cloudformation for apigateway
我在 apicloudformation 的网关模板
中为我的 GET
方法配置了以下配置
"paths": {
"/customer/{customerid}": {
"get": {
"description": "Returns JSON customer objects from DynamoDB.",
"parameters": [
{
"required": true,
"type": "string",
"name": "customerid",
"in": "path"
}
],
"produces": [
"application/json"
],
"x-amazon-apigateway-integration": {
"passthroughBehavior": "never",
"responses": {
"default": {
"statusCode": "200"
}
},
"uri": {
"Fn::Join": [
":",
[
"arn",
"aws",
"apigateway",
{
"Ref": "AWS::Region"
},
"dynamodb",
"action/GetItem"
]
]
},
"httpMethod": "POST",
"requestTemplates": {
"application/json": "{\n \"TableName\": \"customer\",\n \"Key\": {\n \"customerid\": {\n \"S\": \"$input.params('customerid')\"\n }\n }\n}\n"
},
"credentials": {
"Fn::GetAtt": [
"TableAccessRole",
"Arn"
]
},
"type": "aws"
},
"consumes": [
"application/json"
],
"responses": {
"200": {
"description": "200 response"
}
}
}
}
正在完美创建api,但是api的响应是
{
"Item": {
"Name": {
"S": "Alex"
},
"CustomerId": {
"S": "123"
}
}
}
但我希望它像
一样简单 json
{
"Name":"Alex",
"CustomerId":"123"
}
我正在查看 aws 文档,但我无法弄清楚我的配置的哪一部分需要更改。我知道我有 input
变量可以用来获取数据,但是在哪里以及如何,我迷路了
由于您现有的模板使用了您已经定义的x-amazon-apigateway-integration
Swagger extension, you can add a responseTemplates
object containing your response mapping template to the existing default
response
:
"x-amazon-apigateway-integration": {
"passthroughBehavior": "never",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "{\"Name\": \"$input.path('$.Item.Name.S')\", \"CustomerId\": \"$input.path('$.Item.CustomerId.S')\"}"
}
}
},
[...]
我在 apicloudformation 的网关模板
中为我的GET
方法配置了以下配置
"paths": {
"/customer/{customerid}": {
"get": {
"description": "Returns JSON customer objects from DynamoDB.",
"parameters": [
{
"required": true,
"type": "string",
"name": "customerid",
"in": "path"
}
],
"produces": [
"application/json"
],
"x-amazon-apigateway-integration": {
"passthroughBehavior": "never",
"responses": {
"default": {
"statusCode": "200"
}
},
"uri": {
"Fn::Join": [
":",
[
"arn",
"aws",
"apigateway",
{
"Ref": "AWS::Region"
},
"dynamodb",
"action/GetItem"
]
]
},
"httpMethod": "POST",
"requestTemplates": {
"application/json": "{\n \"TableName\": \"customer\",\n \"Key\": {\n \"customerid\": {\n \"S\": \"$input.params('customerid')\"\n }\n }\n}\n"
},
"credentials": {
"Fn::GetAtt": [
"TableAccessRole",
"Arn"
]
},
"type": "aws"
},
"consumes": [
"application/json"
],
"responses": {
"200": {
"description": "200 response"
}
}
}
}
正在完美创建api,但是api的响应是
{
"Item": {
"Name": {
"S": "Alex"
},
"CustomerId": {
"S": "123"
}
}
}
但我希望它像
一样简单 json{
"Name":"Alex",
"CustomerId":"123"
}
我正在查看 aws 文档,但我无法弄清楚我的配置的哪一部分需要更改。我知道我有 input
变量可以用来获取数据,但是在哪里以及如何,我迷路了
由于您现有的模板使用了您已经定义的x-amazon-apigateway-integration
Swagger extension, you can add a responseTemplates
object containing your response mapping template to the existing default
response
:
"x-amazon-apigateway-integration": {
"passthroughBehavior": "never",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "{\"Name\": \"$input.path('$.Item.Name.S')\", \"CustomerId\": \"$input.path('$.Item.CustomerId.S')\"}"
}
}
},
[...]