AWS APIGateway 集成响应映射模板未根据 Content-Type 正确切换

AWS APIGateway Integration Response Mapping Templates do not switch correctly based on Content-Type

我正在试验 AWS API-Gateway 与 S3 后端的集成。我注意到 integration-response 中不同 mapping-templates 之间的切换似乎不起作用。

在integration-response中,我有以下映射模板:

application/json:

{
  type: "JSON",
  body: "$input.body"
}

text/plain:

PlainText:
$input.body

我无能为力似乎无法使用 text/plain 映射模板 - 它总是使用 application/json.

我希望 text/plain 映射模板的使用基于以下条件之一:

  1. S3 returns 内容 Content-Type: text/plain.
  2. 对 API-Gateway 的初始请求传递 Accept: text/plain header。

按照下面的示例,S3 returns a Content-Type: text/plain 并且我使用 Accept: text/plain 请求。 API-Gateway 也正确响应 Content-Type: text/plain。 然而 application/json 模板仍然用于转换 body.

我什至从 method-response 中完全删除了 application/json 作为有效响应类型,但仍然没有。

知道为什么会这样吗?

仅供参考,我使用的是经典的 v1 ApiGateway(休息)。

Execution log for request c24cfce3-2cf3-4693-ad72-4fdf44f4fdcd
Wed May 19 17:13:38 UTC 2021 : Starting execution for request: c24cfce3-2cf3-4693-ad72-4fdf44f4fdcd
Wed May 19 17:13:38 UTC 2021 : HTTP Method: GET, Resource Path: /feeds-poc/test.txt
Wed May 19 17:13:38 UTC 2021 : Method request path: {filename=test.txt}
Wed May 19 17:13:38 UTC 2021 : Method request query string: {}
Wed May 19 17:13:38 UTC 2021 : Method request headers: {Accept=text/plain}
Wed May 19 17:13:38 UTC 2021 : Method request body before transformations: 
Wed May 19 17:13:38 UTC 2021 : Endpoint request URI: https://my-bucket-id.s3.eu-west-1.amazonaws.com/test.txt
Wed May 19 17:13:38 UTC 2021 : Endpoint request headers: {Authorization=****57173a, X-Amz-Date=20210519T171338Z, x-amzn-apigateway-api-id=123456789a, Accept=application/json, User-Agent=AmazonAPIGateway_123456789a, X-Amz-Security-Token=**** [TRUNCATED]
Wed May 19 17:13:38 UTC 2021 : Endpoint request body after transformations: 
Wed May 19 17:13:38 UTC 2021 : Sending request to https://my-bucket-id.s3.eu-west-1.amazonaws.com/test.txt
Wed May 19 17:13:38 UTC 2021 : Received response. Status: 200, Integration latency: 43 ms
Wed May 19 17:13:38 UTC 2021 : Endpoint response headers: {x-amz-id-2=****, x-amz-request-id=****, Date=Wed, 19 May 2021 17:13:39 GMT, Last-Modified=Mon, 17 May 2021 16:45:13 GMT, ETag="420f804aa21220bf0db57bb4b9799c8a", Accept-Ranges=bytes, Content-Type=text/plain, Content-Length=13, Server=AmazonS3}
Wed May 19 17:13:38 UTC 2021 : Endpoint response body before transformations: It's working

Wed May 19 17:13:38 UTC 2021 : Method response body after transformations: {
  type: "JSON",
  body: "It's working
"
}
Wed May 19 17:13:38 UTC 2021 : Method response headers: {X-Amzn-Trace-Id=Root=****, Content-Type=text/plain}
Wed May 19 17:13:38 UTC 2021 : Successfully completed execution
Wed May 19 17:13:38 UTC 2021 : Method completed with status: 200

AWS Support 向我确认该切换是基于原始请求 Accept header。使用 Postman 对此进行测试可确认此功能。

但是 API-Gateway 的 AWS 控制台中内置的测试工具似乎无法满足测试响应映射模板的需求 - 来自 AWS 支持:the purpose of API Gateway test console is only to test the Integration, it doesn't work for end-to-end request.