kperez
kperez

Reputation: 31

UnicodeEncodeError: 'latin-1' codec can't encode character u'\u03c7' in position 66: ordinal not in range(256)

I sometimes get this error on my web server.

Traceback (most recent call last):
File "/bin/user_wsgi_wrapper.py", line 130, in __call__
    self.error_log_file.logger.exception("Error running WSGI application")
File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
    self.error(msg, *args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
    self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
    self.threadName = threading.current_thread().name
File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
    return _active[_get_ident()]
File "/bin/user_wsgi_wrapper.py", line 122, in __call__
    app_iterator = self.app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1690, in wsgi_app
    return response(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 1082, in __call__
    app_iter, status, headers = self.get_wsgi_response(environ)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 1072, in get_wsgi_response
    return app_iter, self.status, headers.to_list()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/datastructures.py", line 1141, in to_list
    for k, v in self]
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u03c7' in position 66: ordinal not in range(256)

The problem is that I have no idea where this happens in my code, so I don't know where I should put the .encode('utf-8', 'ignore').

Upvotes: 2

Views: 2452

Answers (2)

davidism
davidism

Reputation: 127180

You appear to be trying to set a header that has a character not in the ISO-8859-1 character set. Currently, HTTP headers must only contain characters from that codec. More recent versions of Werkzeug require the Latin-1 codec instead to more closely follow the WSGI spec.

See this Flask issue for a message about this from the maintainer. It mentions that the correct way to use characters outside the range is described in RFC 5987, which may be implemented in the future.

Upvotes: 6

GhitaB
GhitaB

Reputation: 3437

Put a:

import pdb; pdb.set_trace()

in: File "/bin/user_wsgi_wrapper.py", line 130, in call

Restart the app and check vars values in your terminal. Press "n" for "next". Use "print var_name" to see a value for a variable. Read more here: https://docs.python.org/2/library/pdb.html

Upvotes: 0

Related Questions