gae request body is empty
事情起因,做一个接口,对方返回的内容有中文,而且编码问题,在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 版本关系可能性大一些。
在网上找了好久,还没找到答案。暂时在这记录一下。
0
See Also
Nearby
- 上一篇 › 百度老不收录,怎么办。。
- 下一篇 › 请问列表页可以调用内容重点图片吗?
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
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.