pythonunicodebottle

bottle convert post request to unicode


I have a server as app and all works ok except when I save ajax forms. If I save from python script - with right input - data are returned as unicode. But the data from js is strange: on pipe should be only bytes(that's the only data type http knows) , but bottle show me str (it is not utf-8) and I can't encode/decode to get correct value. On js side I try with jquery and form.serialise, works for other frameworks.

@post('/agt/save')
def saveagt():
    a = Agent({x: request.forms.get(x) for x in request.forms})
    print(a.nume, a.nume.encode())
    return {'ret': ags.add(a)}

... and a name like „țânțar” become „ÈânÈar”.

It may be a simple problem, but I think I didn't drink enough coffee yet.


Solution

  • If anyone is curious, bottle don't handle corect the url. So urllib.parse.unquote(request.body.read().decode()) solve problem. or

    d = urllib.parse.parse_qs(request.body.read().decode())
    a = Agent({x: d[x][0] for x in d})
    

    in my case.

    Is it a bug of bottle? Or should I tell him to decode URI and I don't know how?