mov*_*yer 6 content-type aws-api-gateway
我已设置 AWS API Gateway 将请求传递到返回图像的服务。
当我在 UI 中使用“测试”功能时,日志显示方法响应中返回的 PNG 数据以及 `Content-Type=image/png:
但是,当您实际在浏览器中访问端点时,会Content-Type出现application/json. 我希望“测试”UI 的日志中显示的“方法响应标头”与实际返回的内容相匹配。
如何强制 API Gateway 将上游的 Content-Type(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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
事实证明我错过了两件事:
首先,我需要更改 AWS 将在“Accept”标头中发送到上游的类型列表
"x-amazon-apigateway-binary-media-types" : [
"image/png"
]
Run Code Online (Sandbox Code Playgroud)
其次,我需要将集成响应设置为“转换为二进制(如果需要)”:
"contentHandling": "CONVERT_TO_BINARY"
Run Code Online (Sandbox Code Playgroud)
这是修改后的配置:
{
"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"
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14160 次 |
| 最近记录: |