Search code examples
python-3.xrequesttornadochunks

Read data in chunks from large request_body


I am new to tornado . Currently I want to read from a post request_body . But data in post request is large so I want to implement it through stream_body_request in Tornado. But I am not able to implement it. How to read this 'img_data' in chunks?

@stream_request_body
class MyReportPDF(BaseHandler):
async def post(self):
    data = escape.json_decode(self.get_body_argument('img_data'))#This data is an base_64_array
    for i in range(len(data)):
        # Clean the base_64 Image
        data[i].replace('data:image/jpeg;base64,', '')
        decode_image.append(base64.b64decode(data[i]))

Solution

  • When you use @stream_request_body, your RequestHandler class should define a method data_received which will be called with data as it comes in. The post method is called at the end after all of the data is received.

    Note that it is not currently possible to use the argument-related methods with @stream_request_body; you'll need to parse the data in data_received as it comes in. This means that if possible you'll want to structure your API to receive the image as a plain HTTP PUT instead of being wrapped in a form-style POST.