2016-10-14 12:51:08 +01:00
|
|
|
# Request Data
|
|
|
|
|
|
|
|
## Properties
|
|
|
|
|
|
|
|
The following request variables are accessible as properties:
|
|
|
|
|
2016-10-15 19:09:16 +01:00
|
|
|
`request.files` (dictionary of File objects) - List of files that have a name, body, and type
|
|
|
|
`request.json` (any) - JSON body
|
|
|
|
`request.args` (dict) - Query String variables. Use getlist to get multiple of the same name
|
|
|
|
`request.form` (dict) - Posted form variables. Use getlist to get multiple of the same name
|
2016-10-21 11:55:30 +01:00
|
|
|
`request.body` (bytes) - Posted raw body. To get the raw data, regardless of content type
|
2017-01-16 23:27:50 +00:00
|
|
|
`request.ip` (str) - IP address of the requester
|
2016-10-14 12:51:08 +01:00
|
|
|
|
|
|
|
See request.py for more information
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```python
|
|
|
|
from sanic import Sanic
|
2016-10-21 11:55:30 +01:00
|
|
|
from sanic.response import json, text
|
2016-10-14 12:51:08 +01:00
|
|
|
|
|
|
|
@app.route("/json")
|
|
|
|
def post_json(request):
|
|
|
|
return json({ "received": True, "message": request.json })
|
|
|
|
|
|
|
|
@app.route("/form")
|
|
|
|
def post_json(request):
|
|
|
|
return json({ "received": True, "form_data": request.form, "test": request.form.get('test') })
|
|
|
|
|
|
|
|
@app.route("/files")
|
|
|
|
def post_json(request):
|
|
|
|
test_file = request.files.get('test')
|
|
|
|
|
|
|
|
file_parameters = {
|
|
|
|
'body': test_file.body,
|
|
|
|
'name': test_file.name,
|
|
|
|
'type': test_file.type,
|
|
|
|
}
|
|
|
|
|
|
|
|
return json({ "received": True, "file_names": request.files.keys(), "test_file_parameters": file_parameters })
|
|
|
|
|
|
|
|
@app.route("/query_string")
|
|
|
|
def query_string(request):
|
|
|
|
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
|
2016-10-21 11:55:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/users", methods=["POST",])
|
|
|
|
def create_user(request):
|
|
|
|
return text("You are trying to create a user with the following POST: %s" % request.body)
|
2016-10-15 19:09:16 +01:00
|
|
|
```
|