1

My code is:

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)

What I see is:

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

How do I get the form data?

** EDIT ** As an aside, that's if I send data with x-www-form-urlencoded. If I send as form-data, I get:

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
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Shamoon
  • 41,293
  • 91
  • 306
  • 570

1 Answers1

2

Traditionally you would create a cgi.FieldStorage object, which reads stdin (usually - there are CGI standards about what it does and when). That's a bit passé nowadays. Urlparse.parse_qs is designed to convert from form data to 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']}
>>>
holdenweb
  • 33,305
  • 7
  • 57
  • 77