Skip to content

Instantly share code, notes, and snippets.

@gabrielpjordao
Created December 11, 2012 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielpjordao/4259170 to your computer and use it in GitHub Desktop.
Save gabrielpjordao/4259170 to your computer and use it in GitHub Desktop.
Problem with special characters in URL
Traceback (most recent call last):
File "/env/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/env/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/env/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/env/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/env/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/env/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/url_problem/urlproblem.py", 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/environment.py", 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__)
@app.route('/test')
def test():
env = app.jinja_env
template = env.from_string('{{url}}')
return template.render(url=flask.request.url)
if __name__ == '__main__':
app.run(port=5001, 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