openfaas / python-flask-template

HTTP and Flask-based OpenFaaS templates for Python 3
MIT License
85 stars 86 forks source link

Update README example for accessing headers #40

Closed mehyedes closed 4 years ago

mehyedes commented 4 years ago

Updated README example for accessing the headers with the python3-http template

Signed-off-by: Mehdi Yedes mehdi.yedes@gmail.com

Description

Motivation and Context

Using event.headers['Some-Header'] throws an error when calling a python3-http function if the header is not set. So it is better to use event.headers.get('Some-Header') instead.

How Has This Been Tested?

After deploying this small function

def handle(event, context):
    header = event.headers.get('Test-Header')
    return {
        "statusCode": 200,
        "body": {
            "Header received": header
        }
    }

The function would not fail, but only return null when the header Test-Header is not set

$ curl 127.0.0.1:8080/function/python-test -H "Test-Header: testtest"
{"Header received":"testtest"}

$ curl 127.0.0.1:8080/function/python-test 
{"Header received":null}

Types of changes

Checklist: