I have a Python WebApp that accepts a POSTed AJAX array. I can access the array fine:
print self.request.POST.items()
#(new lines in response for readability)
[(u'verb', u'create'),
(u'my_id', u'18'),
(u'properties[1]', u'1000'),
(u'properties[3]', u'2000')]
Crucially, the properties supplied are a) of an unknown quantity, and b) are not always sequential.
At the moment I collect them all with this:
i = 0
while i <= 50:
current_key = 'properties['+str(i)+']'
if self.request.get(current_key) != "":
self.response.out.write(self.request.get(current_key))
i = i + 1
But I feel disgusting about myself while I'm doing it (and most of the time there are fewer than 50 properties, and some day there might be more... I'm not sure there could be a sloppier, less Pythonic solution).
Any tips on what I'm missing here? If I try to access 'properties' directly (the plan was to iteritems over it or something) it's unset, which makes me think I'm just looking at a collection of disconnected strings named 'properties[1]' and 'properties[3]' which aren't actually an array / list / dictionary at all.
I do have access to the js at the other end if that's a neater fix - at the moment it goes something like:
var properties = {1:1000, 2:2000, 4:3000};
$.post( "/api", { verb: 'create', my_id: my_id, properties: properties })
Send the data as JSON. Then you can deserialize directly to a dict, without any processing needed.
$.post( "/api", { verb: 'create', my_id: my_id, properties: JSON.stringify(properties) })
...
properties = json.loads(self.request.get('properties', {}))