Python bottle requests and unicode
request.query['q']
and forms.get('q')
return the raw byte value submitted by the web browser. The value äöü
, submitted by a browser as UTF-8 encoded bytes, is '\xc3\xa4\xc3\xb6\xc3\xbc'
.
If you print that byte string, and the place you're printing it to interprets it as ISO-8859-1, or the similar Windows code page 1252, you will get äöü
. If you are debugging by printing to a Windows command prompt, or a file displayed by Notepad, that's why.
If you use the alternative direct property access request.query.q
or forms.q
Bottle will give you Unicode strings instead, decoded from the byte version using UTF-8. It's usually best to work with these Unicode strings wherever you can. (Though still you may have trouble printing them to console. The Windows command prompt is notoriously terrible at coping with non-ASCII characters, and as such is a bad place to be debugging Unicode issues.)