Aws api gateway 如何在AWS API网关中传递内容类型?
我已经设置了AWSAPI网关,将请求传递给返回图像的服务 当我在UI中使用“测试”功能时,日志显示方法响应中返回的PNG数据,以及`Content Type=image/PNG: 但是,当您实际在浏览器中访问端点时,Aws api gateway 如何在AWS API网关中传递内容类型?,aws-api-gateway,content-type,Aws Api Gateway,Content Type,我已经设置了AWSAPI网关,将请求传递给返回图像的服务 当我在UI中使用“测试”功能时,日志显示方法响应中返回的PNG数据,以及`Content Type=image/PNG: 但是,当您实际在浏览器中访问端点时,内容类型是application/json。 我希望“测试”UI的日志中显示的“方法响应头”与实际返回的内容相匹配 如何强制API网关将上游的内容类型(image/png,在本例中,但其他更一般)返回到浏览器 以下是Swagger 2.0语法中定义的端点: "/format/{i
内容类型是application/json
。
我希望“测试”UI的日志中显示的“方法响应头”与实际返回的内容相匹配
如何强制API网关将上游的内容类型(image/png
,在本例中,但其他更一般)返回到浏览器
以下是Swagger 2.0语法中定义的端点:
"/format/{id}/image.png": {
"get": {
"tags": [],
"summary": "",
"deprecated": true,
"operationId": "get-png",
"produces": [
"image/png"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "My Description",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Successful operation",
"schema": {
"type": "file"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string",
"description": "URI that may access the resource"
},
"Content-Type": {
"type": "string",
"description": "Response MIME type"
}
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Access-Control-Allow-Origin": "'*'",
"method.response.header.Content-Type": "integration.response.header.Content-Type"
}
}
},
"requestParameters": {
"integration.request.path.id": "method.request.path.id"
},
"uri": "https://[image_service]/{id}.png",
"passthroughBehavior": "when_no_match",
"httpMethod": "GET",
"type": "http"
}
}
}
注:
- 这个端点稍微简化了(但仍然说明了问题)。然而,在现实中,端点还有更多功能(即,我不仅代理请求,还重写路径+查询参数)。
- 如中所述,如果您的端点只是将请求代理到映像服务器,那么您可能应该改用AWS CloudFront。它的价格中包含了边缘缓存,而且大约便宜3倍
如果您想重写路径/查询参数和/或HTTP响应,可以使用AWS lambda侦听来自上游web服务器的“客户端响应”事件,并在其中设置最终的HTTP响应头等。结果表明,我遗漏了两件事:
首先,我需要更改AWS将在“Accept”标题中发送到上游的类型列表
其次,我需要将集成响应设置为“转换为二进制(如果需要)”:
以下是修改后的配置:
{
"swagger": "2.0",
"info": {
"description": "My description",
"title": "My Title",
"version": "1.0.0"
},
"schemes": [
"https",
"http"
],
"paths": {
"/format/{id}/image.png": {
"get": {
"tags": [],
"summary": "My Description",
"deprecated": true,
"operationId": "get-png",
"produces": [
"image/png"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Successful operation",
"schema": {
"type": "file"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string",
"description": "URI that may access the resource"
},
"Content-Type": {
"type": "string",
"description": "Response MIME type"
}
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Content-Type": "integration.response.header.Content-Type",
"method.response.header.Access-Control-Allow-Origin": "'*'"
},
"contentHandling": "CONVERT_TO_BINARY"
}
},
"requestParameters": {
"integration.request.path.id": "method.request.path.id"
},
"uri": "https://img.shields.io/pypi/format/{id}.png",
"passthroughBehavior": "when_no_match",
"httpMethod": "GET",
"type": "http"
}
}
}
},
"definitions": {},
"x-amazon-apigateway-binary-media-types" : [
"image/png"
]
}
在这种情况下,响应是从上游返回的,带有正确的头,我添加了映射以允许它们通过。但是测试UI中显示的头与浏览器实际从网关返回的头不匹配。这里不需要Lambda,也没有任何迹象表明如果您st将其连接在“集成响应”和“方法响应”之间
"contentHandling": "CONVERT_TO_BINARY"
{
"swagger": "2.0",
"info": {
"description": "My description",
"title": "My Title",
"version": "1.0.0"
},
"schemes": [
"https",
"http"
],
"paths": {
"/format/{id}/image.png": {
"get": {
"tags": [],
"summary": "My Description",
"deprecated": true,
"operationId": "get-png",
"produces": [
"image/png"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Successful operation",
"schema": {
"type": "file"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string",
"description": "URI that may access the resource"
},
"Content-Type": {
"type": "string",
"description": "Response MIME type"
}
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Content-Type": "integration.response.header.Content-Type",
"method.response.header.Access-Control-Allow-Origin": "'*'"
},
"contentHandling": "CONVERT_TO_BINARY"
}
},
"requestParameters": {
"integration.request.path.id": "method.request.path.id"
},
"uri": "https://img.shields.io/pypi/format/{id}.png",
"passthroughBehavior": "when_no_match",
"httpMethod": "GET",
"type": "http"
}
}
}
},
"definitions": {},
"x-amazon-apigateway-binary-media-types" : [
"image/png"
]
}