使用urllib2进行SOAP POST,但我一直收到错误

时间:2011-03-05 22:25:23

标签: python api soap post typeerror

我正在尝试通过SOAP POST进行API调用,而且我一直在努力 “TypeError:不是有效的非字符串序列或映射对象。” @ data = urllib.urlencode(values)

SM_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <AutotaskIntegrations xmlns="http://Autotask.net/ATWS/v1_5/">
      <PartnerID>partner id</PartnerID>
    </AutotaskIntegrations>
  </soap:Header>
  <soap:Body>
    <getThresholdAndUsageInfo xmlns="http://Autotask.net/ATWS/v1_5/">
    </getThresholdAndUsageInfo>
  </soap:Body>
</soap:Envelope>"""

values = SM_TEMPLATE%()
data = urllib.urlencode(values)
req = urllib2.Request(site, data)
response = urllib2.urlopen(req)
the_page = response.read()

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

urllib.urlencode函数需要一系列键值对或映射类型,如dict

>>> urllib.urlencode([('a','1'), ('b','2'), ('b', '3')])
'a=1&b=2&b=3'

要执行SOAP HTTP POST,您应该保留SM_TEMPLATE blob,并将其设置为POST正文,然后为POST正文的编码和字符集添加Content-Type标头。例如:

data = SM_TEMPLATE
headers = {
    'Content-Type': 'application/soap+xml; charset=utf-8'
    }
req = urllib2.Request(site, data, headers)

答案 1 :(得分:0)

以下面的代码为例,它可以帮助您使用urllib2 for Python 2.6.6解决SOAP请求。它适用于我调用Oracle Data Integrator(Oracle ODI)。显然,您必须根据您的情况调整值:

<Grid HorizontalAlignment="Center" Width="1000">

 <GroupBox Header = "This stuff is in GroupBox ">
   <Label> Label in GroupBox </Label>
   <Label> Some other label in GroupBox  </Label>
 </GroupBox>

 <Label> This is not in the groupbox so don't put me in it! <Label>

</Grid>

我们将不胜感激任何反馈: - )