【发布时间】:2018-08-28 06:48:38
【问题描述】:
我已设置 AWS API Gateway 以将请求传递给返回图像的服务。
当我在 UI 中使用“测试”功能时,日志会显示方法响应中返回的 PNG 数据,以及 `Content-Type=image/png:
但是,当您实际在浏览器中访问端点时,Content-Type 是application/json。
我本来希望“测试”用户界面的日志中显示的“方法响应标头”与实际返回的内容相匹配。
如何强制 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"
}
}
}
注意事项:
- 此端点有些简化(但仍说明了问题)。然而实际上,端点还有更多内容(即,我不仅代理请求,还重写路径+查询参数)。
- 如this answer 中所述,如果您的终端节点只是代理对图像服务器的请求,您可能应该改用 AWS CloudFront。它的价格中包含边缘缓存,而且便宜约 3 倍。
【问题讨论】:
标签: aws-api-gateway content-type