gae request body is empty

By ego008 at 2013-05-14 15:42:03 • 935次点击

事情起因,做一个接口,对方返回的内容有中文,而且编码问题,在GAE 端解析POST 数据出现错误:

File "/python27_runtime/python27_lib/versions/third_party/webob-1.2.3/webob/request.py", line 783, in POST
    vars = MultiDict.from_fieldstorage(fs)
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.2.3/webob/multidict.py", line 92, in from_fieldstorage
    obj.add(field.name, decode(value))
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.2.3/webob/multidict.py", line 78, in <lambda>
    decode = lambda b: b.decode(charset)
  File "/python27_runtime/python27_dist/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)


'utf8' codec can't decode byte 0xd2 in position 0: invalid continuation byte

于是想自己处理POST 数据,先直接获取 selft.request.body 内容,再自己解析。
在本地测试,向目标url post 一些数据, selft.request.body 有内容,但上线后却发现是空的。
貌似python、wbapp、webob 版本的问题,与python 版本关系可能性大一些。
在网上找了好久,还没找到答案。暂时在这记录一下。

gae, request, body, is, empty


mcdonc:

It's hard to tell what encoding it is in, but the character that can't be decoded is inside "versi\xf3n".

If you need to handle this use:

request = request.transcode(whatevertheencodingis)

within your Pyramid view before doing anything else with the request.

source https://github.com/Pylons/webob/issues/28
https://github.com/Pylons/webob/blob/master/webob/request.py

ego008 at 2013-05-18 12:24:30
1

Haitao :
Another lesson I have learned is that when you deal with text input

within <form> tags, use request.str_GET[ ] and request.str_POST[ ]

instead of request.get(). request.get() can return either unicode

string or encoded byte string, based on charset header sent by client.

But FF and IE have different behaviors with that header, and you will

get UnicodeDecodeError here and there. str_GET and str_POST return the

raw string and you can always call decode() to get unicode string.

ego008 at 2013-05-23 17:42:46
2
登录 后发表评论