在 AWS Cloudformation 上获取 Content-Type 错误

getting error on AWS Cloudformation for Content-Type

我在 AWS Cloudformation 中收到以下错误:

验证结果:警告:[],错误:[指定的内容类型无效:method.response.header.Content-类型](服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;

rootProxy:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      ResourceId: !Ref rootResource
      RestApiId: !Ref apigatewayRestApi
      AuthorizationType: NONE
      HttpMethod: ANY
      
      RequestParameters:
        method.request.header.Content-Type: false
        method.request.path.proxy: false
        
      Integration:
        PassthroughBehavior: WHEN_NO_MATCH
        RequestParameters:
          integration.request.path.proxy: method.request.path.proxy
          integration.request.header.Content-Type: method.request.header.Content-Type
        IntegrationResponses:
          - 
              StatusCode: 200
              ResponseTemplates: 
                method.response.header.Content-Type: "integration.response.header.Content-Type"
              
            
        Type: AWS
        Credentials: "arn:aws:iam::xxx:user/ali@xxx.com"
        IntegrationHttpMethod: ANY
        Uri: !Sub 
          - arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SSRlambdaArn}/invocations
          - SSRlambdaArn: !GetAtt SSR.Arn

      MethodResponses:
        -
          StatusCode: 200
          ResponseModels:
            application/json: Empty
            text/plain: Empty
          ResponseParameters:
            method.response.header.Content-Type: true

对这个错误有什么想法吗?

对于 ResponseTemplates,您应该指定 Velocity 模板语言代码并更正 Content-Type 模板所属的代码。

所以代替:

        IntegrationResponses:
          - 
              StatusCode: 200
              ResponseTemplates: 
                method.response.header.Content-Type: "integration.response.header.Content-Type"
              

你应该有这样的东西(只是一个例子;需要修改为你的API):

            ResponseTemplates: 
              application/json: |
                  #set($inputRoot = $input.path('$'))
                  [
                  #foreach($elem in $inputRoot)
                    {
                      "number": $elem.id,
                      "class": "$elem.type",
                      "salesPrice": $elem.price 
                    }#if($foreach.hasNext),#end

                  #end
                  ]    

或者您可能根本不需要它。其具体应用如何以及是否使用 ResponseTemplates.