Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Problem with special characters in URL
Traceback (most recent call last):
File "/env/lib/python2.7/site-packages/flask/", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/env/lib/python2.7/site-packages/flask/", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/env/lib/python2.7/site-packages/flask/", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/env/lib/python2.7/site-packages/flask/", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/env/lib/python2.7/site-packages/flask/", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/env/lib/python2.7/site-packages/flask/", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/url_problem/", line 9, in test
return template.render(url=flask.request.url)
File "/env/lib/python2.7/dist-packages/Jinja2-2.6-py2.7.egg/jinja2/", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "<template>", line 1, in top-level template code
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 34: ordinal not in range(128)
import flask
app = flask.Flask(__name__)
def test():
env = app.jinja_env
template = env.from_string('{{url}}')
return template.render(url=flask.request.url)
if __name__ == '__main__':, debug=True)
#If we access a URL with special characters with a browser like Chrome, it'll automatically escape the characters
#but with old or problematic browsers (like IE), these characters are not escaped and it raises an error...
#to simulate it, we can use curl, from the command line, with the above program running on 5001.
#$ curl "http://localhost:5001/test?arg=tesét"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment