Inside “Hello World!”

bn create creates a local Python file called Let’s take a look inside:

def handler(body, req):
    name = req.query.get('name') or body.get('name') or 'World'
    return 'Hello {}!'.format(name)

The function receives two arguments. body holds the HTTP request body, and req holds HTTP request and response parameters, like query string and headers. The Returned object is automatically JSON-ified and sent back as the HTTP response body.

Read more about function signatures in the reference.

Sending arguments to the function

# bn invoke, body
$ bn invoke hello --data '{"name": "globe"}'
"Hello globe!"

# curl, query string
$ curl '<KEY>/hello?name=qs'
"Hello qs!"

# curl, body
$ curl '<KEY>/hello' -d '{"name": "body"}'
"Hello body!"

Modifying the code

Let’s tweak the function a bit to return the input string in reverse order. First, let’s change the return statement:

return 'Hello {}!'.format(name.upper())

Now, let’s deploy and invoke:

$ bn deploy hello
$ bn invoke hello --data '{"name": "globe"}'
"Hello GLOBE!"

What next?