ego008 avatar

gae request body is empty

🕣 by ego008

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

1
2
3
4
5
6
7
8
9
10
11
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 版本关系可能性大一些。 在网上找了好久,还没找到答案。暂时在这记录一下。

💘 相关文章

评论

共2条关于"gae request body is empty"的评论

ego008 avatar
#2 ego008 回复

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.

写一条评论

Based on Golang + fastHTTP + sdb | go1.17.1 Processed in 1ms