2016-10-14 12:51:08 +01:00
# Request Data
2017-01-20 03:18:52 +00:00
When an endpoint receives a HTTP request, the route function is passed a
`Request` object.
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
The following variables are accessible as properties on `Request` objects:
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
- `json` (any) - JSON body
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
```python
from sanic.response import json
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00:00
@app .route("/json")
def post_json(request):
return json({ "received": True, "message": request.json })
```
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00:00
- `args` (dict) - Query string variables. A query string is the section of a
URL that resembles `?key1=value1&key2=value2` . If that URL were to be parsed,
2017-04-02 02:28:16 +01:00
the `args` dictionary would look like `{'key1': ['value1'], 'key2': ['value2']}` .
2017-01-20 03:18:52 +00:00
The request's `query_string` variable holds the unparsed string value.
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
```python
from sanic.response import json
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00:00
@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-14 12:51:08 +01:00
2017-04-02 02:28:16 +01:00
- `raw_args` (dict) - On many cases you would need to access the url arguments in
a less packed dictionary. For same previous URL `?key1=value1&key2=value2` , the
`raw_args` dictionary would look like `{'key1': 'value1', 'key2': 'value2'}` .
2017-01-20 03:18:52 +00:00
- `files` (dictionary of `File` objects) - List of files that have a name, body, and type
```python
from sanic.response import json
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00:00
@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,
}
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
return json({ "received": True, "file_names": request.files.keys(), "test_file_parameters": file_parameters })
```
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
- `form` (dict) - Posted form variables.
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
```python
from sanic.response import json
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00:00
@app .route("/form")
def post_json(request):
return json({ "received": True, "form_data": request.form, "test": request.form.get('test') })
```
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
- `body` (bytes) - Posted raw body. This property allows retrieval of the
request's raw data, regardless of content type.
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00:00
```python
from sanic.response import text
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00: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)
```
2017-04-02 02:28:16 +01:00
2017-01-20 03:18:52 +00:00
- `ip` (str) - IP address of the requester.
2016-10-14 12:51:08 +01:00
2017-02-23 16:42:59 +00:00
- `app` - a reference to the Sanic application object that is handling this request. This is useful when inside blueprints or other handlers in modules that do not have access to the global `app` object.
2017-02-23 16:47:39 +00:00
```python
from sanic.response import json
from sanic import Blueprint
2017-02-23 16:42:59 +00:00
2017-02-23 16:47:39 +00:00
bp = Blueprint('my_blueprint')
2017-02-23 16:42:59 +00:00
2017-02-23 16:47:39 +00:00
@bp .route('/')
async def bp_root(request):
if request.app.config['DEBUG']:
return json({'status': 'debug'})
else:
return json({'status': 'production'})
2017-02-23 16:42:59 +00:00
2017-02-23 16:47:39 +00:00
```
2017-03-03 16:59:33 +00:00
- `url` : The full URL of the request, ie: `http://localhost:8000/posts/1/?foo=bar`
- `scheme` : The URL scheme associated with the request: `http` or `https`
- `host` : The host associated with the request: `localhost:8080`
2017-03-03 19:51:13 +00:00
- `path` : The path of the request: `/posts/1/`
2017-03-03 16:59:33 +00:00
- `query_string` : The query string of the request: `foo=bar` or a blank string `''`
2017-02-23 16:42:59 +00:00
2017-01-20 03:18:52 +00:00
## Accessing values using `get` and `getlist`
2016-10-14 12:51:08 +01:00
2017-01-20 03:18:52 +00:00
The request properties which return a dictionary actually return a subclass of
`dict` called `RequestParameters` . The key difference when using this object is
the distinction between the `get` and `getlist` methods.
2016-10-21 11:55:30 +01:00
2017-01-20 03:18:52 +00:00
- `get(key, default=None)` operates as normal, except that when the value of
the given key is a list, *only the first item is returned* .
- `getlist(key, default=None)` operates as normal, *returning the entire list* .
```python
from sanic.request import RequestParameters
2016-10-21 11:55:30 +01:00
2017-01-20 03:18:52 +00:00
args = RequestParameters()
args['titles'] = ['Post 1', 'Post 2']
args.get('titles') # => 'Post 1'
args.getlist('titles') # => ['Post 1', 'Post 2']
2016-10-15 19:09:16 +01:00
```