Consistent with the document
This commit is contained in:
parent
e31de53e2f
commit
4ae796df8f
|
@ -10,6 +10,7 @@ A simple example using default setting would be like this:
|
||||||
```python
|
```python
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.config import LOGGING
|
from sanic.config import LOGGING
|
||||||
|
from sanic.response import text
|
||||||
|
|
||||||
# The default logging handlers are ['accessStream', 'errorStream']
|
# The default logging handlers are ['accessStream', 'errorStream']
|
||||||
# but we change it to use other handlers here for demo purpose
|
# but we change it to use other handlers here for demo purpose
|
||||||
|
@ -20,7 +21,7 @@ app = Sanic('test')
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return response.text('Hello World!')
|
return text('Hello World!')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(log_config=LOGGING)
|
app.run(log_config=LOGGING)
|
||||||
|
|
|
@ -5,70 +5,70 @@ Use functions in `sanic.response` module to create responses.
|
||||||
## Plain Text
|
## Plain Text
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import text
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text')
|
@app.route('/text')
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.text('Hello world!')
|
return text('Hello world!')
|
||||||
```
|
```
|
||||||
|
|
||||||
## HTML
|
## HTML
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import html
|
||||||
|
|
||||||
|
|
||||||
@app.route('/html')
|
@app.route('/html')
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.html('<p>Hello world!</p>')
|
return html('<p>Hello world!</p>')
|
||||||
```
|
```
|
||||||
|
|
||||||
## JSON
|
## JSON
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import json
|
||||||
|
|
||||||
|
|
||||||
@app.route('/json')
|
@app.route('/json')
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.json({'message': 'Hello world!'})
|
return json({'message': 'Hello world!'})
|
||||||
```
|
```
|
||||||
|
|
||||||
## File
|
## File
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import file
|
||||||
|
|
||||||
|
|
||||||
@app.route('/file')
|
@app.route('/file')
|
||||||
async def handle_request(request):
|
async def handle_request(request):
|
||||||
return await response.file('/srv/www/whatever.png')
|
return await file('/srv/www/whatever.png')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Streaming
|
## Streaming
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import stream
|
||||||
|
|
||||||
@app.route("/streaming")
|
@app.route("/streaming")
|
||||||
async def index(request):
|
async def index(request):
|
||||||
async def streaming_fn(response):
|
async def streaming_fn(response):
|
||||||
response.write('foo')
|
response.write('foo')
|
||||||
response.write('bar')
|
response.write('bar')
|
||||||
return response.stream(streaming_fn, content_type='text/plain')
|
return stream(streaming_fn, content_type='text/plain')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Redirect
|
## Redirect
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import redirect
|
||||||
|
|
||||||
|
|
||||||
@app.route('/redirect')
|
@app.route('/redirect')
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.redirect('/json')
|
return redirect('/json')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Raw
|
## Raw
|
||||||
|
@ -76,12 +76,12 @@ def handle_request(request):
|
||||||
Response without encoding the body
|
Response without encoding the body
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import raw
|
||||||
|
|
||||||
|
|
||||||
@app.route('/raw')
|
@app.route('/raw')
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.raw('raw data')
|
return raw('raw data')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Modify headers or status
|
## Modify headers or status
|
||||||
|
@ -89,12 +89,12 @@ def handle_request(request):
|
||||||
To modify headers or status code, pass the `headers` or `status` argument to those functions:
|
To modify headers or status code, pass the `headers` or `status` argument to those functions:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sanic import response
|
from sanic.response import json
|
||||||
|
|
||||||
|
|
||||||
@app.route('/json')
|
@app.route('/json')
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.json(
|
return json(
|
||||||
{'message': 'Hello world!'},
|
{'message': 'Hello world!'},
|
||||||
headers={'X-Served-By': 'sanic'},
|
headers={'X-Served-By': 'sanic'},
|
||||||
status=200
|
status=200
|
||||||
|
|
Loading…
Reference in New Issue
Block a user