Skip to content

Instantly share code, notes, and snippets.

@tiivik
Created March 16, 2019 15:28
Show Gist options
  • Save tiivik/cf43b96357f28407fdcfc06179561673 to your computer and use it in GitHub Desktop.
Save tiivik/cf43b96357f28407fdcfc06179561673 to your computer and use it in GitHub Desktop.
Supporting code snippets for API Gateway request validation

Lambda function Code

import json

def lambda_handler(event, context):
    body = json.loads(event["body"])
    data = {
        "cars": [
            {
                "id": 0,
                "status": "en_route"
            },
            {
                "id": 1,
                "status": "on_standby"
            }
        ],
        "trucks": [
            {
                "id": 0,
                "status": "on_standby"
            },
            {
                "id": 1,
                "status": "en_route"
            }
        ]
    }
   
    category = body["category"]
    
    return {
        'statusCode': 200,
        'body': json.dumps(data[category])
    }

Lambda Test

{
  "body": "{\"category\":\"cars\"}"
}

API Gateway Model Schema

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "FleetAPI",
    "type": "object",
    "properties": {
        "category": {
            "type": "string",
            "enum": ["cars", "trucks"]
        }
    },
    "required": ["category"]
}

POST to deployed API

curl -H 'Content-Type: application/json' -d '{"category":"cars"}' https://XXX.execute-api.XXX.amazonaws.com/development
curl -H 'Content-Type: application/json' -d '{"category":"trucks"}' https://XXX.execute-api.XXX.amazonaws.com/development
curl -H 'Content-Type: application/json' -d '{"category":"trains"}' https://XXX.execute-api.XXX.amazonaws.com/development
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment