如何在AWS API Gateway中传递Content-Type?

mov*_*yer 6 content-type aws-api-gateway

我已设置 AWS API Gateway 将请求传递到返回图像的服务。

AWS UI 中显示的终端节点

当我在 UI 中使用“测试”功能时,日志显示方法响应中返回的 PNG 数据以及 `Content-Type=image/png:

“测试”UI 的 AWS 日志显示内容类型正确

但是,当您实际在浏览器中访问端点时,会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 CloudFront。它的价格包含边缘缓存,并且便宜约 3 倍。

mov*_*yer 5

事实证明我错过了两件事:

首先,我需要更改 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)