I am serving a PNG or SVG image through Flask. Locally it works fine but when I run the application inside docker and send request (POST) I get following error:
RuntimeError: Attempted implicit sequence conversion but the response object is in direct passthrough mode.
Bellow code for serving PIL image through flask:
def serve_image(image: Image, mime_type: FileFormat, download: bool):
suffix = mime_type.value.split('/')[-1]
temp_file = tempfile.TemporaryFile(mode='w+b', suffix=suffix)
if suffix == 'png':
image.save(temp_file, suffix)
else:
# we cant force svg extension in PIL
image.save(temp_file)
temp_file.seek(0, 0)
return send_file(temp_file, mimetype=mime_type.value, as_attachment=download,
attachment_filename='img.' + suffix)
I have tried using BytesIO no luck there either. Setting
Response.implicit_sequence_conversion = False
Response.direct_passthrough = False
or
@app.after_request
def after_request_func(r):
r.direct_passthrough = False
r.implicit_sequence_conversion = False
return r
Did not help either.
The problem was in openapi-core Flask validation, and it was solved by creating werkzeug Response with direct passtrough set to False. Other headers like attachment cache-timeout had to be set manually.
res = Response(temp_file, direct_passthrough=False, mimetype=mime_type.value)