Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending multiple POST data items with the same name, using AppEngine

I try to send POST data to a server using urlfetch in AppEngine. Some of these POST-data items has the same name, but with different values.

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 receieve only one item named data, with the value bar. How could I solve this problem?

like image 262
nip3o Avatar asked Aug 26 '10 16:08

nip3o


1 Answers

Modify your form_fields dictionary so that fields with the same name are turned into lists, and use the doseq argument to urllib.urlencode:

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

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

At this point, form_data is 'data=foo&data=bar', which is what I think you need.

like image 61
Will McCutchen Avatar answered Sep 19 '22 00:09

Will McCutchen