Sanic allows you to get request data by stream, as below. When the request ends, awaitrequest.stream.read() returns None. Only post, put and patch decorator have stream argument.

fromsanicimportSanicfromsanic.viewsimportCompositionViewfromsanic.viewsimportHTTPMethodViewfromsanic.viewsimportstreamasstream_decoratorfromsanic.blueprintsimportBlueprintfromsanic.responseimportstream,textbp=Blueprint('blueprint_request_stream')app=Sanic('request_stream')classSimpleView(HTTPMethodView):@stream_decoratorasyncdefpost(self,request):result=''whileTrue:body=awaitrequest.stream.read()ifbodyisNone:breakresult+=body.decode('utf-8')returntext(result)@app.post('/stream',stream=True)asyncdefhandler(request):asyncdefstreaming(response):whileTrue:body=awaitrequest.stream.read()ifbodyisNone:breakbody=body.decode('utf-8').replace('1','A')awaitresponse.write(body)returnstream(streaming)@bp.put('/bp_stream',stream=True)asyncdefbp_put_handler(request):result=''whileTrue:body=awaitrequest.stream.read()ifbodyisNone:breakresult+=body.decode('utf-8').replace('1','A')returntext(result)# You can also use `bp.add_route()` with stream argumentasyncdefbp_post_handler(request):result=''whileTrue:body=awaitrequest.stream.read()ifbodyisNone:breakresult+=body.decode('utf-8').replace('1','A')returntext(result)bp.add_route(bp_post_handler,'/bp_stream',methods=['POST'],stream=True)asyncdefpost_handler(request):result=''whileTrue:body=awaitrequest.stream.read()ifbodyisNone:breakresult+=body.decode('utf-8')returntext(result)app.blueprint(bp)app.add_route(SimpleView.as_view(),'/method_view')view=CompositionView()view.add(['POST'],post_handler,stream=True)app.add_route(view,'/composition_view')if__name__=='__main__':app.run(host='127.0.0.1',port=8000)

Sanic allows you to stream content to the client with the stream method. This method accepts a coroutine callback which is passed a StreamingHTTPResponse object that is written to. A simple example is like follows:

This is useful in situations where you want to stream content to the client that originates in an external service, like a database. For example, you can stream database records to the client with the asynchronous cursor that asyncpg provides:

Sanic provides sanic.response.file_stream function that is useful when you want to send a large file. It returns a StreamingHTTPResponse object and will use chunked transfer encoding by default; for this reason Sanic doesn’t add Content-Length HTTP header in the response. If you want to use this header, you can disable chunked transfer encoding and add it manually: