3

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!

dda
  • 6,030
  • 2
  • 25
  • 34
monoceres
  • 4,722
  • 4
  • 38
  • 63

2 Answers2

1

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

dda
  • 6,030
  • 2
  • 25
  • 34
Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
0

There are some issues in sending http header values in utf-8. Take a look at this question How to send non-English unicode string using HTTP header?.

Community
  • 1
  • 1
Ifthikhan
  • 1,484
  • 10
  • 14
  • The link mentions that ISO 8859-1 is ok, and since my characters are in ISO 8859-1, can I use this instead? – monoceres Nov 19 '12 at 08:55
  • @monoceres Encode the string in "ISO 8859-1" and try it out. Most likely it should work fine. – Ifthikhan Nov 19 '12 at 09:13
  • No, ISO-8859-1 doesn't work reliably; some UAs default to something else (UTF-8, page encoding of referring page, or system locale). – Julian Reschke Nov 19 '12 at 11:37