Search code examples
pythonpython-3.xflaskunicodepython-unicode

How to correctly read and encode a text sent via flask?


I wrote a simple API that reads and writes files. A minimal version is below:

import flask

class Web:

    def __init__(self):
        self.app = flask.Flask('web')
        self.app.add_url_rule('/read/<filename>', 'read', self.read)
        self.app.run(host="0.0.0.0", port=6543)

    def read(self, filename: str):
        return open(filename).read()  

Web()

Consider the following file (the one that will be read). It is saved as UTF-8 in Windows Notepad:

ascii
éàçèïê

When trying to download it via curl:

PS > curl http://localhost:6543/read/c5f89f95ef49492c82bdbcc133a5c567
ascii
éàçèïê

I tried to redirect it to a file, it is a mess as well:

enter image description here

It looks like that somewhere in the chain "read file with open()" → "return it via flask" I made a mistake in encoding/decoding Unicode magic.


Solution

  • Try to use this construction:

    open(filename, encoding="utf-8")