1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
========================
Example -- Handling POST
========================
The ``route`` decorator supports a ``methods`` keyword which is the list of HTTP methods as strings.
For example, ``methods=['POST']`` will cause the handler to be invoked when an ``POST`` request is received.
If a handler can support multiple methods the current method can be distinguished with ``request.method``.
Here is our ``"Hello, world!"`` example extended to support setting the name we are saying Hello to via a ``POST`` request with a ``name`` argument.
This also demonstrates the use of the redirect method of the request to redirect back to ``'/'`` after handling the ``POST``.
The most specific handler should be defined first.
So the ``POST`` handler must be defined before the handler with no ``methods``.
.. code-block:: python
from twisted.internet.defer import succeed
from klein import run, route
name = b"world"
@route("/", methods=["POST"])
def setname(request):
global name
name = request.args.get(b'name', [b'world'])[0]
request.redirect('/')
return succeed(None)
@route('/')
def hello(request):
return b"Hello, %s!" % (name,)
run("localhost", 8080)
The following curl command can be used to test this behaviour::
curl -v -L -d name='bob' http://localhost:8080/
Accessing the request content
=============================
To read the content of the request use the ``read`` method of
:api:`twisted.web.iweb.IRequest <IRequest.content>`
.. code-block:: python
from klein import run, route
import json
@route('/', methods=['POST'])
def do_post(request):
content = json.loads(request.content.read())
response = json.dumps(dict(the_data=content), indent=4)
return response
run("localhost", 8080)
The following curl command can be used to test this behaviour::
curl -XPOST -v -H 'Content-Type: appliction/json' -d '{"name":"bob"}' http://localhost:8080/
|