【问题标题】:AWS APIGateway Integration Response Mapping Templates do not switch correctly based on Content-TypeAWS APIGateway 集成响应映射模板不会根据 Content-Type 正确切换
【发布时间】:2023-03-23 12:25:01
【问题描述】:

我正在试验 AWS API-Gateway 与 S3 后端的集成。我注意到集成响应中不同映射模板之间的切换似乎不起作用。

在集成响应中,我有以下映射模板:

应用程序/json:

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

文本/纯文本:

PlainText:
$input.body

我似乎无法使用 text/plain 映射模板 - 它始终使用 application/json

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

  1. S3 返回带有Content-Type: text/plain 的内容。
  2. 对 API-Gateway 的初始请求传递一个 Accept: text/plain 标头。

根据下面的示例,S3 返回一个Content-Type: text/plain AND 我用Accept: text/plain 请求。 API-Gateway 也正确响应 Content-Type: text/plain。 但是application/json 模板仍然用于转换body。

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

任何想法为什么会发生这种情况?

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

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

【问题讨论】:

    标签: amazon-web-services amazon-s3 aws-api-gateway integration


    【解决方案1】:

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

    AWS 控制台中内置的 API-Gateway 测试工具似乎不适合测试响应映射模板 - 来自 AWS 支持:the purpose of API Gateway test console is only to test the Integration, it doesn't work for end-to-end request

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 2021-12-08
      • 2015-12-28
      • 2018-05-02
      • 2018-01-20
      • 2018-07-31
      • 2021-04-20
      • 2019-07-19
      • 1970-01-01
      相关资源
      最近更新 更多