【问题标题】:getting error on AWS Cloudformation for Content-Type在 AWS Cloudformation 上出现 Content-Type 错误
【发布时间】:2020-11-10 07:19:24
【问题描述】:

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

验证结果:警告:[],错误:[指定的内容类型无效:method.response.header.Content-Type](服务: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

关于这个错误的任何想法?

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cloudformation aws-api-gateway content-type


    【解决方案1】:

    对于ResponseTemplates,您应该指定速度模板语言代码并更正模板所属的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

    【讨论】:

    • 如此令人印象深刻,我在官方文档中找不到像您这样的文档和示例。谢谢。
    • @AliSadeghipourKorabaslo 这基于tutorial。该教程不在CFN中,但我前段时间将其翻译为CFN以满足我的需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2019-08-17
    • 2021-09-07
    • 2022-08-18
    • 1970-01-01
    • 2020-10-09
    相关资源
    最近更新 更多