1

Using Tornado in Python 3.5, I have a problem setting Unicode character for HTTP Header. I need it to set the download file name in unicide (Content-Disposition header). But the following error would be occurred:

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 42-46: ordinal not in range(256)

According to Tornado documentation, in python 2, a decode to latin1 and re-encode to utf8 will perform if non-ascii characters are used. But in python 3 the following line cause the exception:

lines.extend(l.encode('latin1') for l in header_lines)
mtoloo
  • 1,795
  • 3
  • 22
  • 28
  • HTTP headers do not allow non-ASCII characters. You have to use an ASCII-compatible encoding, such as RFC 2047/2231, RFC 8187, etc. Different HTTP servers, and even different browsers, support different encoding formats. For instance, [see this blog](http://blog.softartisans.com/2013/06/03/how-to-download-files-containing-special-or-non-ascii-characters-in-the-filename-using-asp-net/) as well as [this StackOverflow question](https://stackoverflow.com/questions/93551/). – Remy Lebeau Dec 19 '17 at 18:12
  • 1
    But in python 2.7, I am able to set a unicode file name to be downloaded – mtoloo Dec 20 '17 at 05:29

0 Answers0