【问题标题】:How to get content-encoding from ApiGateway caching resource如何从 ApiGateway 缓存资源中获取内容编码
【发布时间】:2020-10-01 18:18:24
【问题描述】:

我们有一个返回 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.js amazon-cloudformation aws-api-gateway serverless


    【解决方案1】:

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

    method.response.header.Content-Encoding: true
    

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

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

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

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

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

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

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

    【讨论】:

    • 更改集成响应参数如下失败 cloudformation method.response.header.Content-Encoding: 'gzip'
    • 这成功了。我仍然不知道如何让它传递值而不是静态地声明它。 method.response.header.Content-Encoding: "'gzip'"
    • 要动态传递值,您需要创建映射模板逻辑或从服务端点返回值,或者让初始调用在请求中包含正确的值。
    • 你能帮我创建一个映射模板吗? @pkarfs
    猜你喜欢
    • 2021-04-24
    • 2020-10-19
    • 2018-11-21
    • 2012-04-10
    • 1970-01-01
    • 2012-12-01
    • 2011-05-22
    • 2011-03-10
    • 2018-06-13
    相关资源
    最近更新 更多