public
Last active

Problem with special characters in URL

  • Download Gist
gistfile1.pytb
Python traceback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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)
urlproblem.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.