【问题标题】:How to pass through Content-Type in AWS API Gateway?如何通过 AWS API Gateway 中的 Content-Type?
【发布时间】:2018-08-28 06:48:38
【问题描述】:

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

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

但是,当您实际在浏览器中访问端点时,Content-Typeapplication/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


    【解决方案1】:

    原来我遗漏了两件事:

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

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

    其次,我需要将集成响应设置为“转换为二进制(如果需要)”:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 在这种情况下,响应从上游返回并带有正确的标头,并且我添加了映射以允许它们通过。但是测试 UI 中显示的标头与浏览器实际从网关返回的标头不匹配。这里不需要 Lambda,也没有任何迹象表明如果您只是将它连接在“集成响应”和“方法响应”之间,它会对结果产生任何影响
      猜你喜欢
      • 2021-10-30
      • 2020-04-18
      • 2018-07-24
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 2019-10-20
      • 1970-01-01
      相关资源
      最近更新 更多