Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode in Flask content-disposition header

I'm trying to set the Content-Disposition header in a Flask response object to a filenam which may contain Swedish characters (åäö). My test code looks like this:

response =  flask.send_file(output_file_path)
response.headers[u"Content-Disposition"] = u'filename="åäö.pdf"'

This presents the following error:

File "C:\Python27\Lib\BaseHTTPServer.py", line 401, in send_header
self.wfile.write("%s: %s\r\n" % (keyword, value))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 10: ordinal
not in range(128)

I've tried encoding, decoding and just about anything I can think about, but I just cannot get Flask to accept that I want to use Unicode (utf-8) in my header!

like image 645
monoceres Avatar asked Aug 05 '26 12:08

monoceres


1 Answers

See RFCs 6266 for Content-Disposition and 5987 for encoding. Test cases here.

like image 191
Julian Reschke Avatar answered Aug 07 '26 01:08

Julian Reschke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!