如何通过API网关传递路径变量

时间:2016-03-21 20:03:29

标签: api amazon-web-services swagger gateway

这是我目前在Swagger文件中的内容:

"/v1/user/{username}": {
      "get": {
        "consumes": ["application/json"],
        "produces": ["application/json"],
        "parameters": [{
          "name": "username",
          "in": "path",
          "description": "Username of user",
          "required": true,
          "type": "string"
        }],
        "responses": {
          "200": {
            "description": "user retrieved",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/User"
              }
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "type": "http",
          "uri": "http://useroregon.recopspipeline.com/v1/user/{username}",
          "httpMethod": "GET",
          "responses": {
            "default": {
              "statusCode": "200",
              "responseTemplates": {
                "application/json": "$input.json('$.body')"
              }
            }
          }
        }
      },

但我的后端只收到“{username}”作为路径变量 任何人都知道如何发送路径变量?
有关如何在swagger json文件中或通过GUI执行此操作的提示非常棒!

1 个答案:

答案 0 :(得分:1)

此OpenAPI(fka.Swagger)文件错过了请求参数与代理服务(requestParameters中的x-amazon-apigateway-integration)之间的映射。

 "x-amazon-apigateway-integration": {
      "type": "http",
      "uri": "http://useroregon.recopspipeline.com/v1/user/{username}",
      "requestParameters": {
        "integration.request.path.username": "method.request.path.username"
      }
      "httpMethod": "GET",
      "responses": {
        "default": {
          "statusCode": "200",
          "responseTemplates": {
            "application/json": "$input.json('$.body')"
          }
        }
      }
    }

如果使用API​​网关控制台设计API并使用代理操作并导出它,则可以获得:

swagger: "2.0"
info:
  title: "Proxy"

schemes:
- "https"

paths:
  /test/{username}:
    get:
      produces:
      - "application/json"
      parameters:
      - name: "username"
        in: "path"
        required: true
        type: "string"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
        requestParameters:
          integration.request.path.username: "method.request.path.username"
        passthroughBehavior: "when_no_match"
        httpMethod: "GET"
        uri: "http://somedomain.com/{username}"
        type: "http"

definitions:
  Empty:
    type: "object"

您的定义也缺少passthroughBehavior: "when_no_match",但可能不是问题。