2018-08-28 13:58:03 +00:00
|
|
|
|
|
|
|
Starlette includes a `Request` class that gives you a nicer interface onto
|
|
|
|
the incoming request, rather than accessing the ASGI scope and receive channel directly.
|
|
|
|
|
|
|
|
### Request
|
|
|
|
|
|
|
|
Signature: `Request(scope, receive=None)`
|
|
|
|
|
|
|
|
```python
|
2018-09-05 09:29:04 +00:00
|
|
|
from starlette.requests import Request
|
2018-08-29 10:17:09 +00:00
|
|
|
from starlette.response import Response
|
|
|
|
|
|
|
|
|
2018-08-28 13:58:03 +00:00
|
|
|
class App:
|
|
|
|
def __init__(self, scope):
|
2019-01-25 14:40:33 +00:00
|
|
|
assert scope['type'] == 'http'
|
2018-08-28 13:58:03 +00:00
|
|
|
self.scope = scope
|
|
|
|
|
|
|
|
async def __call__(self, receive, send):
|
|
|
|
request = Request(self.scope, receive)
|
|
|
|
content = '%s %s' % (request.method, request.url.path)
|
|
|
|
response = Response(content, media_type='text/plain')
|
|
|
|
await response(receive, send)
|
|
|
|
```
|
|
|
|
|
|
|
|
Requests present a mapping interface, so you can use them in the same
|
|
|
|
way as a `scope`.
|
|
|
|
|
|
|
|
For instance: `request['path']` will return the ASGI path.
|
|
|
|
|
|
|
|
If you don't need to access the request body you can instantiate a request
|
|
|
|
without providing an argument to `receive`.
|
|
|
|
|
|
|
|
#### Method
|
|
|
|
|
|
|
|
The request method is accessed as `request.method`.
|
|
|
|
|
|
|
|
#### URL
|
|
|
|
|
|
|
|
The request URL is accessed as `request.url`.
|
|
|
|
|
2018-10-05 10:29:06 +00:00
|
|
|
The property is a string-like object that exposes all the
|
2018-08-28 13:58:03 +00:00
|
|
|
components that can be parsed out of the URL.
|
|
|
|
|
|
|
|
For example: `request.url.path`, `request.url.port`, `request.url.scheme`.
|
|
|
|
|
|
|
|
#### Headers
|
|
|
|
|
|
|
|
Headers are exposed as an immutable, case-insensitive, multi-dict.
|
|
|
|
|
|
|
|
For example: `request.headers['content-type']`
|
|
|
|
|
|
|
|
#### Query Parameters
|
|
|
|
|
2019-01-23 09:04:33 +00:00
|
|
|
Query parameters are exposed as an immutable multi-dict.
|
2018-08-28 13:58:03 +00:00
|
|
|
|
2018-10-29 09:22:45 +00:00
|
|
|
For example: `request.query_params['search']`
|
|
|
|
|
|
|
|
#### Path Parameters
|
|
|
|
|
|
|
|
Router path parameters are exposed as a dictionary interface.
|
|
|
|
|
|
|
|
For example: `request.path_params['username']`
|
2018-08-28 13:58:03 +00:00
|
|
|
|
2018-12-14 09:58:27 +00:00
|
|
|
#### Client Address
|
|
|
|
|
|
|
|
The client's remote address is exposed as a named two-tuple `request.client`.
|
|
|
|
Either item in the tuple may be `None`.
|
|
|
|
|
|
|
|
The hostname or IP address: `request.client.host`
|
|
|
|
|
|
|
|
The port number from which the client is connecting: `request.client.port`
|
|
|
|
|
2018-10-17 10:15:32 +00:00
|
|
|
#### Cookies
|
|
|
|
|
2018-10-17 10:17:07 +00:00
|
|
|
Cookies are exposed as a regular dictionary interface.
|
2018-10-17 10:15:32 +00:00
|
|
|
|
|
|
|
For example: `request.cookies.get('mycookie')`
|
|
|
|
|
2018-08-28 13:58:03 +00:00
|
|
|
#### Body
|
|
|
|
|
|
|
|
There are a few different interfaces for returning the body of the request:
|
|
|
|
|
|
|
|
The request body as bytes: `await request.body()`
|
|
|
|
|
2018-10-12 17:15:04 +00:00
|
|
|
The request body, parsed as form data or multipart: `await request.form()`
|
|
|
|
|
2018-08-28 13:58:03 +00:00
|
|
|
The request body, parsed as JSON: `await request.json()`
|
|
|
|
|
|
|
|
You can also access the request body as a stream, using the `async for` syntax:
|
|
|
|
|
|
|
|
```python
|
2018-09-05 09:29:04 +00:00
|
|
|
from starlette.requests import Request
|
|
|
|
from starlette.responses import Response
|
2018-08-29 10:17:09 +00:00
|
|
|
|
|
|
|
|
2018-08-28 13:58:03 +00:00
|
|
|
class App:
|
|
|
|
def __init__(self, scope):
|
2019-01-25 14:40:33 +00:00
|
|
|
assert scope['type'] == 'http'
|
2018-08-28 13:58:03 +00:00
|
|
|
self.scope = scope
|
|
|
|
|
|
|
|
async def __call__(self, receive, send):
|
|
|
|
request = Request(self.scope, receive)
|
|
|
|
body = b''
|
|
|
|
async for chunk in request.stream():
|
|
|
|
body += chunk
|
|
|
|
response = Response(body, media_type='text/plain')
|
|
|
|
await response(receive, send)
|
|
|
|
```
|
|
|
|
|
|
|
|
If you access `.stream()` then the byte chunks are provided without storing
|
2018-11-14 10:58:05 +00:00
|
|
|
the entire body to memory. Any subsequent calls to `.body()`, `.form()`, or `.json()`
|
2018-10-12 17:15:04 +00:00
|
|
|
will raise an error.
|
2019-01-15 09:59:59 +00:00
|
|
|
|
|
|
|
In some cases such as long-polling, or streaming responses you might need to
|
|
|
|
determine if the client has dropped the connection. You can determine this
|
|
|
|
state with `disconnected = await request.is_disconnected()`.
|
2019-02-19 13:14:53 +00:00
|
|
|
|
|
|
|
#### Other state
|
|
|
|
|
|
|
|
If you want to store additional information on the request you can do so
|
|
|
|
using `request.state`.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
`request.state.time_started = time.time()`
|