python httplib2 POST:如何设置'post参数'?

时间:2012-08-29 19:44:00

标签: python post urllib2

我在使用带有urllib2请求的urllib2.open向网站发布xml时出错。我将data参数设置为我需要发布的xml字符串,但我得到的错误是“XML数据未在POST参数中发送”xml“。

是否有人知道如何设置数据以使其显示在命名的“post参数”中?

感谢您的帮助

代码是这样的:

req = urllib2.request(url=theurl,data = xml,headers = {'Content-type': 'application/xml'}
response = urllib2.urlopen(req)

其中xml只是一个带有原始xml的字符串

如果我打印req.data,它会提供我要发布的xml。但是接收站点想要一个名为'xml'的POST参数。我不知道如何控制它。

1 个答案:

答案 0 :(得分:1)

data应该是'parameter_name': value - 字典

的字典
url = 'http://www.someserver.com/some/handler'
values = {'name' : 'Some Name',
          'location' : 'SomeCity',
          'xml': your_xml}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)