我的代码是:
class GameSession(JSONMessageHandler, UserHandlingMixin):
@allow_all
def post(self):
s = self.s = Session()
payload = self.message.body
# payload = json.loads(payload)
print 'payload start'
print payload
print 'payload end'
current_gamesession = self.check_gamesession(payload.prize_id,
payload.publisher_id)
我看到的是:
payload start
prize_id=2&publisher_id=32&foreign_user_id=1234
payload end
ERROR:root:'str' object has no attribute 'prize_id'
Traceback (most recent call last):
File "/home/vagrant/src/brubeck/brubeck/request_handling.py", line 338, in __call__
rendered = fun(*self._url_args)
File "/vagrant/mysite/api/views.py", line 31, in wrapper
return func(self, *args, **kwargs)
File "/vagrant/mysite/api/views.py", line 629, in post
如何获取表单数据?
**编辑**
顺便说一句,如果我使用x-www-form-urlencoded
发送数据。如果我发送form-data
,我会收到:
payload start
------WebKitFormBoundaryFX1GuivvAA42T3uk
Content-Disposition: form-data; name="prize_id"
2
------WebKitFormBoundaryFX1GuivvAA42T3uk
Content-Disposition: form-data; name="publisher_id"
1
------WebKitFormBoundaryFX1GuivvAA42T3uk
Content-Disposition: form-data; name="foreign_user_id"
2321
------WebKitFormBoundaryFX1GuivvAA42T3uk--
payload end
答案 0 :(得分:2)
传统上你会创建一个cgi.FieldStorage
对象,它读取标准输入(通常 - 有关于它的作用和时间的CGI标准)。现在有点过时了。 Urlparse.parse_qs
旨在将表单数据转换为dict:
>>> import urlparse
>>> urlparse.parse_qs("prize_id=2&publisher_id=32&foreign_user_id=1234")
{'prize_id': ['2'], 'foreign_user_id': ['1234'], 'publisher_id': ['32']}
>>>