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

时间:2018-03-19 16:11:44

标签: aws-api-gateway content-type

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

The endpoint as it appears in the AWS UI

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

The AWS log of the "Test" UI showing Content-Type being correct

但是,当您实际在浏览器中访问该端点时,Content-Typeapplication/json。 我希望“测试”用户界面的日志中显示“方法响应标题”,以匹配实际返回的内容。

如何强制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"
    }
  }
}

注意:

  • 此端点有些简化(但仍然说明了问题)。但实际上,端点还有更多(即我不只是代理请求,还包括重写路径+查询参数)。
    • this answer所述,如果您的端点只是代理对图像服务器的请求,则应该使用AWS CloudFront。它的价格中包含边缘缓存,价格便宜约3倍。

2 个答案:

答案 0 :(得分:2)

原来我错过了两件事:

首先,我需要更改AWS将在“接受”标题中发送到上游的类型列表“

"x-amazon-apigateway-binary-media-types" : [
  "image/png"
]

其次,我需要将Integration Response设置为“Convert to binary(如果需要)”:

"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"
  ]

}

答案 1 :(得分:0)

如果要重写路径/查询参数和/或HTTP响应,可以使用AWS lambda监听来自上游Web服务器的“客户端响应”事件,并在那里设置最终的HTTP响应头等。