data = {
'ids': [12, 3, 4, 5, 6 , ...]
}
urllib2.urlopen("http://abc.example/api/posts/create",urllib.urlencode(data))
我想发送 POST 请求,但其中一个字段应该是数字列表。我怎样才能做到这一点? (JSON?)
如果您的服务器期望 POST 请求为 json,那么您需要添加标头,并序列化请求的数据...
Python 2.x
import json
import urllib2
data = {
'ids': [12, 3, 4, 5, 6]
}
req = urllib2.Request('http://example.com/api/posts/create')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(data))
Python 3.x
https://stackoverflow.com/a/26876308/496445
如果不指定标题,它将是默认的application/x-www-form-urlencoded
type.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)