aws-api-gateway - 指定的映射表达式参数无效

标签 aws-api-gateway

我正在尝试为 AWS API Gateway 部署编写 Swagger 配置,并为示例(主要从文档中复制)提出了这个配置:

{
  "swagger": "2.0",
  "info": {
    "description": "desc",
    "title": "TestAPI",
    "version": "1.0"
  },
  "schemes": [
    "https"
  ],
  "paths": {
    "/": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "test",
            "headers": {
              "Content-type": {
                "type": "string"
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "aws",
          "uri" : "[REDACTED]",
          "credentials" : "[REDACTED]",
          "httpMethod" : "POST",
          "requestTemplates" : {
            "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
          },
          "requestParameters" : {
            "integration.request.querystring.stage" : "method.request.querystring.version",
            "integration.request.querystring.provider" : "method.request.querystring.vendor"
          },
          "responses" : {
            "2\\d{2}" : {
              "statusCode" : "200",
              "responseParameters" : {
                "method.response.header.requestId" : "integration.response.header.cid"
              },
              "responseTemplates" : {
                "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
              }
            },
            "302" : {
              "statusCode" : "302",
              "responseParameters" : {
                "method.response.header.Location" : "integration.response.body.redirect.url"
              }
            },
            "default" : {
              "statusCode" : "400",
              "responseParameters" : {
                "method.response.header.test-method-response-header" : "'static value'"
              }
            }
          }
        }
      }
    }
  }
}

但问题是
aws apigateway import-rest-api --body 'file://deploy/api.json' --region eu-west-1 

输出以下内容:
An error occurred (BadRequestException) when calling the ImportRestApi operation: Errors found during import:
    Unable to put integration on 'GET' for resource at path '/': Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression parameter specified: method.request.querystring.version]

该部分直接取自 documentation ,所以这对我来说真的很困惑。任何想法该怎么做?文档似乎在很多方面都缺乏,因此很难用它解决很多问题。

最佳答案

您是否尝试过定义 versionvendor作为方法的参数?

{
  "swagger": "2.0",
  "info": {
    "description": "desc",
    "title": "TestAPI",
    "version": "1.0"
  },
  "schemes": [
    "https"
  ],
  "paths": {
    "/": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters" : [
            {
                "name" : "version",
                "in" : "query",
                "required" : true,
                "type" : "string"
                "description" : "The version requested"
            },          
            {
                "name" : "vendor",
                "in" : "query",
                "required" : true,
                "type" : "string"
                "description" : "The vendor being queried"
            }
        ],
        "responses": {
          "200": {
            "description": "test",
            "headers": {
              "Content-type": {
                "type": "string"
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "type" : "aws",
          "uri" : "[REDACTED]",
          "credentials" : "[REDACTED]",
          "httpMethod" : "POST",
          "requestTemplates" : {
            "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
          },
          "requestParameters" : {
            "integration.request.querystring.stage" : "method.request.querystring.version",
            "integration.request.querystring.provider" : "method.request.querystring.vendor"
          },
          "responses" : {
            "2\\d{2}" : {
              "statusCode" : "200",
              "responseParameters" : {
                "method.response.header.requestId" : "integration.response.header.cid"
              },
              "responseTemplates" : {
                "application/json" : "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }"
              }
            },
            "302" : {
              "statusCode" : "302",
              "responseParameters" : {
                "method.response.header.Location" : "integration.response.body.redirect.url"
              }
            },
            "default" : {
              "statusCode" : "400",
              "responseParameters" : {
                "method.response.header.test-method-response-header" : "'static value'"
              }
            }
          }
        }
      }
    }
  }
}

API Gateway 要求方法请求参数为 defined before being referenced

关于aws-api-gateway - 指定的映射表达式参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49158921/

相关文章:

python - 如何使用 Python 请求发送 AWS 凭证?

amazon-web-services - 如何从控制台获取aws apigateway中api的部署ID?

linux - aws api gateway cli 更新集成请求因 $ 参数而失败

amazon-web-services - 您可以在 terraform 子目录之间共享父资源吗?

amazon-web-services - 描述 CloudFormation 中的 AWS API Gateway 正文映射模板

amazon-web-services - AWS SAM 部署 : Error fork/exec/var/task/myfunction : no such file or directory: PathError

javascript - 如何将API Gateway SDK添加到React中?

amazon-web-services - 如何在cloudformation中为api网关自定义响应

node.js - 是否可以将API网关与AWS lambda中的 Node 路由连接?

amazon-web-services - API Gateway 和 Lambda 之间的限制