pythonhttplib2post:如何设置POST参数?

2024-03-28 19:20:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

有人知道如何设置数据使其出现在命名的“post parameter”中吗?在

谢谢你的帮助

代码如下:

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

其中xml只是包含原始xml的字符串

如果我打印需求数据它给出了我要发布的xml。但是接收站点希望在名为“xml”的POST参数中实现这一点。我不知道怎么控制它。在


Tags: 数据字符串代码data参数parameter站点错误
1条回答
网友
1楼 · 发布于 2024-03-28 19:20:00

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)

相关问题 更多 >