2

I'm trying to return the contents of an image file via a Python Connexion application generated from an OpenAPI v2 spec file using swagger-codegen and the python-flask language setting. In my controller module, I simply do the following:

def file_contents_get(file_id):
    file = app.datastore.get_instance().get_file(file_id)
    with open(file.path, "rb") as f:
        return f.read()

However, this results in the following error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

What is the proper way to return a file's contents? Note that I don't want the file as an attachment but rather inline.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
ScoPi
  • 1,193
  • 9
  • 14
  • 1
    So it looks like this was an issue in my API spec where I was describing the produceable content types as "image/gif", "image/jpg", and "image/png". I guess I didn't understand how this really worked, but when I changed the only possible content type to "application/octet-stream", it began to work as I wanted. – ScoPi May 09 '18 at 21:08
  • Would you mind posting your final solution? I'm currently running into exactly the same issues. – Ichwardort Sep 19 '20 at 23:33

0 Answers0