AWS API网关 - HTTP直通路径参数

时间:2017-09-16 04:58:00

标签: amazon-web-services aws-api-gateway

我正在尝试API网关中的HTTP passthrough功能,​​将资源方法传递给另一个API。我想将路径参数从API网关URL传递到后端API,后端API也需要这些路径参数。

我有以下简单的Swagger文档试图测试它:

{
  "swagger": "2.0",
  "info": {
    "version": "2017-09-15T03:33:48Z",
    "title": "api-gateway-http-test"
  },
  "schemes": [
    "https"
  ],
  "paths": {
    "/subresource/{name}": {
      "get": {
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Empty"
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "uri": "http://my.web.service.url/subresource/{name}",
          "passthroughBehavior": "when_no_match",
          "httpMethod": "GET",
          "type": "http_proxy",
          "requestParameters": {
            "integration.request.path.name": "method.request.path.name"
          }
        }
      }
    }
  },
  "definitions": {
    "Empty": {
      "type": "object",
      "title": "Empty Schema"
    }
  }
}

当我尝试通过CloudFormation将其部署到API网关时,API Gateway会给我这个错误:

Unable to put integration on 'GET' for resource at path '/subresource/{name}': 
Invalid mapping expression specified: 
Validation Result: 
warnings : [], errors : [Invalid mapping expression parameter specified: method.request.path.name]

我在线查看了各种来源,这种配置“requestParameters”部分的方式似乎是将路径参数传递给后端API的推荐方法。

我在这里错过了什么会导致这种情况无效?

1 个答案:

答案 0 :(得分:3)

缺少参数定义。

使用以下内容进行检查,

{
  "swagger": "2.0",
  "info": {
    "version": "2017-09-15T03:33:48Z",
    "title": "api-gateway-http-test"
  },
  "schemes": [
    "https"
  ],
  "paths": {
    "/subresource/{name}": {
      "get": {
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Empty"
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "uri": "http://google.com/subresource/{name}",
          "passthroughBehavior": "when_no_match",
          "httpMethod": "GET",
          "type": "http_proxy",
          "requestParameters": {
            "integration.request.path.name": "method.request.path.name"
          }
        }
      }
    }
  },
  "definitions": {
    "Empty": {
      "type": "object",
      "title": "Empty Schema"
    }
  }
}