update request streaming doc

This commit is contained in:
Yun Xu 2018-12-03 22:51:09 -08:00
parent 268d254d85
commit fca7cb9fb0
2 changed files with 6 additions and 6 deletions

View File

@ -2,7 +2,7 @@
## Request Streaming ## Request Streaming
Sanic allows you to get request data by stream, as below. When the request ends, `request.stream.get()` returns `None`. Only post, put and patch decorator have stream argument. Sanic allows you to get request data by stream, as below. When the request ends, `await request.stream.read()` returns `None`. Only post, put and patch decorator have stream argument.
```python ```python
from sanic import Sanic from sanic import Sanic
@ -22,7 +22,7 @@ class SimpleView(HTTPMethodView):
async def post(self, request): async def post(self, request):
result = '' result = ''
while True: while True:
body = await request.stream.get() body = await request.stream.read()
if body is None: if body is None:
break break
result += body.decode('utf-8') result += body.decode('utf-8')
@ -33,7 +33,7 @@ class SimpleView(HTTPMethodView):
async def handler(request): async def handler(request):
async def streaming(response): async def streaming(response):
while True: while True:
body = await request.stream.get() body = await request.stream.read()
if body is None: if body is None:
break break
body = body.decode('utf-8').replace('1', 'A') body = body.decode('utf-8').replace('1', 'A')
@ -45,7 +45,7 @@ async def handler(request):
async def bp_handler(request): async def bp_handler(request):
result = '' result = ''
while True: while True:
body = await request.stream.get() body = await request.stream.read()
if body is None: if body is None:
break break
result += body.decode('utf-8').replace('1', 'A') result += body.decode('utf-8').replace('1', 'A')
@ -55,7 +55,7 @@ async def bp_handler(request):
async def post_handler(request): async def post_handler(request):
result = '' result = ''
while True: while True:
body = await request.stream.get() body = await request.stream.read()
if body is None: if body is None:
break break
result += body.decode('utf-8') result += body.decode('utf-8')

View File

@ -7,7 +7,7 @@ from sanic.response import stream, text
from sanic.request import StreamBuffer from sanic.request import StreamBuffer
data = "abc" * 100000 data = "abc" * 10000000
def test_request_stream_method_view(app): def test_request_stream_method_view(app):