首页 > 解决方案 > 如何从 ApiGateway 缓存资源中获取内容编码

问题描述

我们有一个返回 gzip 编码的端点。我们想要缓存该值,并且我们正在使用 ApiGateway 为我们执行此操作。资源方法定义如下,

GetManifestApiGatewayMethod: # very good
      Type: "AWS::ApiGateway::Method"
      Properties:
        AuthorizationType: "NONE"
        HttpMethod: "GET"
        ResourceId:
          Ref: ManifestConfigurationResource
        RestApiId:
          Ref: ApiGatewayRestApi
        RequestParameters:
          method.request.path.seasonCode: true
          method.request.path.facilityCode: true
          method.request.path.configurationCode: true
          method.request.querystring.policyCode: true
          method.request.header.PAC-Authorization: true
          method.request.header.PAC-Application-ID: true
          method.request.header.PAC-API-Key: true
          method.request.header.PAC-Channel-Code: true
          method.request.header.PAC-Organization-ID: true
          method.request.header.PAC-Developer-ID: true
          method.request.header.PAC-Request-ID: false
          method.request.header.Accept-Encoding: true
        MethodResponses:
          - StatusCode: 200
            # ResponseParameters:
            #   method.response.header.Content-Encoding: true
        Integration:
          IntegrationHttpMethod: GET
          Type: HTTP
          Uri: https://${self:provider.environment.PDI_HOST}/pdi/v1/manifest/{seasonCode}/{facilityCode}/{configurationCode}
          PassthroughBehavior: WHEN_NO_MATCH
          CacheKeyParameters:
            - method.request.path.seasonCode
            - method.request.path.facilityCode
            - method.request.path.configurationCode
            - method.request.querystring.policyCode
          IntegrationResponses:
            - StatusCode: 200
              SelectionPattern: '\d\d\d'
              # ResponseParameters:
              #   method.response.header.content-encoding: integration.response.body.headers.content-encoding
          RequestParameters:
            integration.request.path.seasonCode: method.request.path.seasonCode
            integration.request.path.facilityCode: method.request.path.facilityCode
            integration.request.path.configurationCode: method.request.path.configurationCode
            integration.request.querystring.policyCode: method.request.querystring.policyCode
            integration.request.header.Authorization: method.request.header.PAC-Authorization
            integration.request.header.PAC-Application-ID: method.request.header.PAC-Application-ID
            integration.request.header.PAC-API-Key: method.request.header.PAC-API-Key
            integration.request.header.PAC-Channel-Code: method.request.header.PAC-Channel-Code
            integration.request.header.PAC-Organization-ID: method.request.header.PAC-Organization-ID
            integration.request.header.PAC-Developer-ID: method.request.header.PAC-Developer-ID
            integration.request.header.PAC-Request-ID: method.request.header.PAC-Request-ID
            integration.request.header.Accept-Encoding: method.request.header.Accept-Encoding

http.get 方法包含以下逻辑:

const encoding = response.headers["content-encoding"]; if (encoding && encoding.indexOf("gzip") >= 0) {...} // handle the gzip

但是当我们使用上面的集成方法时,我没有得到通常直接点击它代理的 api 得到的标题。有一些注释代码,我试图将其传递,但internal server error​​是当使用这些响应映射时我得到了。

标签: node.jsamazon-cloudformationaws-api-gatewayserverless

解决方案


从您的代码模板的外观来看,方法响应标头看起来是正确的。

method.response.header.Content-Encoding: true

但是,您的集成响应 ResponseParameters 似乎是错误的。

method.response.header.content-encoding: integration.response.body.headers.content-encoding

首先,属性Key应该完全匹配,因此大写可能会增加问题。但是,您的价值看起来是真正的问题。根据AWS 文档

使用目标作为键,源作为值:

目标必须是 MethodResponse 属性中的现有响应参数。

源必须是现有方法请求参数或静态值。您必须将静态值括在单引号中,并根据请求中指定的目标对这些值进行预编码。

看起来好像您正在尝试将集成响应方法映射到值而不是方法请求参数


推荐阅读