bottle convert post request to unicode

Question:

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.

Asked By: cox

||

Answers:

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?

Answered By: cox

Use

request.forms.getunicode('some_form_field_name')

as shorthand, if you want to get around the character conversion to latin-1.

Answered By: Pebermynte Lars
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.