requestTemplates和responseTemplates在AWS API Gateway SAM模板中不起作用

时间:2020-05-01 18:53:23

标签: aws-api-gateway api-gateway

我在定义要使用的requestTemplates和responseTemplates时遇到问题。

我的(处理过的)模板的相关部分在这里:

/cse:
            options:
              x-amazon-apigateway-integration:
                type: mock
                requestTemplates:
                  application/json: |
                    {
                      "statusCode" : 200
                    }
                responses:
                  default:
                    statusCode: '200'
                    responseTemplates:
                      application/json: |
                        {}
                    responseParameters:
                      method.response.header.Access-Control-Allow-Origin: '''*'''
                      method.response.header.Access-Control-Allow-Methods: '''GET,OPTIONS'''
              summary: CORS support
              responses:
                '200':
                  headers:
                    Access-Control-Allow-Origin:
                      schema:
                        type: string
                    Access-Control-Allow-Methods:
                      schema:
                        type: string
                  description: Default response for CORS method
            get:
              x-amazon-apigateway-integration:
                httpMethod: POST
                requestTemplates:
                  application/json: |
                    {
                      "body": $input.json('$'),
                      "headers": {
                        #foreach($header in $input.params().header.keySet())
                        "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
                          #end
                      },
                      "method": "$context.httpMethod",
                      "params": {
                        #foreach($param in $input.params().path.keySet())
                        "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end
                          #end
                      },
                      "query": {
                        #foreach($queryParam in $input.params().querystring.keySet())
                        "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end
                          #end
                      }
                    }
                type: aws
                responses:
                  default:
                    statusCode: 200
                    responseTemplates:
                      text/html: $input.path('$')
                    responseParameters:
                      method.response.header.Access-Control-Allow-Origin: '''*'''
                      method.response.header.Access-Control-Allow-Methods: '''GET,OPTIONS'''
                uri: !Sub >-
                  arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${CSELambda.Arn}/invocations
              responses:
                '200':
                  headers:
                    Access-Control-Allow-Origin:
                      type: string
                    Access-Control-Allow-Methods:
                      type: string
                  description: OK

AWS通过在模板中添加一个options项来自动生成Cors位,并且工作正常。 了解它如何定义responseTemplatesrequestTemplates并起作用。

get位是我要开始工作的位。据我所知,我使用的格式与自动生成的options位相同,但是API网关未设置这两个模板,我很茫然。

迄今为止,Google一直在使用谷歌搜索技术,但尝试不同的尝试都没有成功。任何帮助表示赞赏。

0 个答案:

没有答案