Search code examples
apiamazon-web-servicesswaggergateway

How to pass a path variable through API Gateway


This is what I currently have in my Swagger file:

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

But my backend just receives "{username}" as the path variable.
Anyone know how to send the path variable along?
Tips on how to do it in a swagger json file or via the GUI would be great!


Solution

  • This OpenAPI (fka. Swagger) file miss the mapping between request parameter and proxied service (requestParameters in 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')"
              }
            }
          }
        }
    

    If you design your API with a proxy operation using the API Gateway console and exporting it you get this:

    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"
    

    Your definition lacks also passthroughBehavior: "when_no_match" but it may not be a problem.