The Python HTTP template gives you additional control of the HTTP response for your function and access to the HTTP request details.
This template is a work in progress and makes use of the incubator project of-watchdog.
$ faas template pull https://github.com/kturcios/python-http-template
$ faas new --lang python2.7-http
The function handler is passed two arguments, event and context.
event contains the prominent data about the request, including:
- body
- headers
- method
- query
- path
context contains basic information about the function, including:
- hostname
By default, flask will automatically attempt to set the correct Content-Type header for you based on the type of response. For example, returning a dict object type will automatically attach the header Content-Type: application/json and returning a string type will automatically attach the Content-Type: text/html, charset=utf-8 for you.
Successful response status code and JSON response body
def handle(event, context):
    return {
        "response": {
            "key": "value"
        },
        "status_code": 200
    }Successful response status code and string response body
def handle(event, context):
    return {
        "response": "Object successfully created",
        "status_code": 201
    }Failure response status code and JSON error message
def handle(event, context):
    return {
        "response": {
            "error": "Bad request"
        },
        "status_code": 400
    }Setting custom response headers
def handle(event, context):
    return {
        "response": {
            "key": "value"
        },
        "status_code": 200,
        "headers": [
            ("Location", "https://www.example.com/")
        ]
    }Accessing request body
def handle(event, context):
    return {
        "response": "You said: " + str(event.body),
        "status_code": 200
    }Accessing request method
def handle(event, context):
    if event.method == 'GET':
        return {
            "response": "GET request",
            "status_code": 200
        }
    else:
        return {
            "response": "Method not allowed",
            "status_code": 405
        }Accessing request query string arguments
def handle(event, context):
    return {
        "response": {
            "name": event.query['name']
        },
        "status_code": 200
    }Accessing request headers
def handle(event, context):
    return {
        "response": {
            "content-type-received": event.headers['Content-Type']
        },
        "status_code": 200
    }