如何在AWS API Gateway中传递Content-Type? [英] How to pass through Content-Type in AWS API Gateway?

查看:447
本文介绍了如何在AWS API Gateway中传递Content-Type?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've set up AWS API Gateway to pass through requests to a service that returns images.

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

When I use the "Test" functionality in the UI, the logs show the PNG data being returned in the method response, as well as the `Content-Type=image/png:

但是,当您实际在浏览器中访问端点时,Content-Typeapplication/json. 我希望测试" UI的日志中显示的方法响应标头"与实际返回的内容相匹配.

However, when you actually go and visit the endpoint in a browser, the Content-Type is application/json. I would have expected that the "Method response headers" displayed in the logs of the "Test" UI to match what would actually be returned.

如何强制API网关将上游的Content-Type(在这种情况下为image/png,但更常见的是其他类型)返回给浏览器?

How do I force API Gateway to return the upstream's Content-Type (image/png in this case, but others more generally) to the browser?

这是Swagger 2.0语法中定义的端点:

Here is the endpoint as defined in the Swagger 2.0 syntax:

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

注意:

  • 此端点得到了一些简化(但仍然可以说明问题).但是实际上,端点还有更多功能(即,我不仅在代理请求,而且还重写路径+查询参数).
    • 此答案中所述,如果您的端点只是将请求发送到图像服务器,则您可能应该使用AWS改为使用CloudFront.价格中包含边缘缓存,价格便宜约3倍.
    • This endpoint is somewhat simplified (but still illustrates the problem). However in reality, there is more to the endpoint (ie. I'm not just proxying the requests, but also rewriting paths + query params).
      • As noted in this answer, if your endpoint is just proxing requests to an image server, you should probably use AWS CloudFront instead. It has edge caching included in its price, and is ~3x cheaper.

      推荐答案

      原来,我缺少两件事:

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

      First, I needed to change the list of types AWS will send to the upstream in the "Accept" header"

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

      第二,我需要将集成响应"设置为转换为二进制(如果需要)":

      Secondly, I needed to set the Integration Response to "Convert to binary (if needed)":

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

      这篇关于如何在AWS API Gateway中传递Content-Type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆