gae request body is empty

By at • 1931次点击

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

File "/python27_runtime/python27_lib/versions/third_party/webob-1.2.3/webob/", line 783, in POST
    vars = MultiDict.from_fieldstorage(fs)
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.2.3/webob/", line 92, in from_fieldstorage
    obj.add(, decode(value))
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.2.3/webob/", line 78, in <lambda>
    decode = lambda b: b.decode(charset)
  File "/python27_runtime/python27_dist/lib/python2.7/encodings/", 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 版本关系可能性大一些。 在网上找了好久,还没找到答案。暂时在这记录一下。

body, gae, request


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.


ego008 at 2013-05-18 12:24

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
登录 后发表评论