Sending multiple POST data items with the same name using AppEngine

I am trying to send POST data to a server using urlfetch in AppEngine. Some of these POST data elements have the same name, but with different meanings.

form_fields = {
   "data": "foo",
   "data": "bar"
}

form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url="http://www.foo.com/", payload=form_data, method=urlfetch.POST, headers={'Content-Type': 'application/x-www-form-urlencoded'})

However, in this example, the server seems to receive only one element with a name datawith a value bar. How can I solve this problem?

+5
source share
2 answers

Modify the dictionary form_fieldsso that fields with the same name are converted to lists, and use the argument doseqfor urllib.urlencode:

form_fields = {
   "data": ["foo","bar"]
}

form_data = urllib.urlencode(form_fields, doseq=True)

At this moment, form_datathere is 'data=foo&data=bar'what I think you need.

+13

python ; - webob.MultiDict:

>>> z = webob.MultiDict([('foo', 'bar'), ('foo', 'baz')])
>>> urllib.urlencode(z)
'foo=bar&foo=baz'
+1

All Articles