2016-03-21 70 views
1

這是我目前在我揚鞭文件:如何通過API傳遞路徑變量網關

"/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')" 
       } 
      } 
      } 
     } 
     }, 

但我的後端剛剛收到「{用戶名}」爲路徑變量。
任何人都知道如何發送路徑變量?
關於如何在swagger json文件或通過GUI做到這一點的提示會很棒!

回答

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"但它可能不會是一個問題。