开发者

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

开发者 https://www.devze.com 2023-01-13 00:37 出处:网络
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.

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?


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.


A normal python dict can't handle this sort of thing; use something like a webob.MultiDict:

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

精彩评论

暂无评论...
验证码 换一张
取 消