我正在尝试通过 SOAP POST 进行 API 调用,但我不断收到 “类型错误:不是有效的非字符串序列或映射对象。” @data = urllib.urlencode(值)
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()
如有任何帮助,我们将不胜感激。
最佳答案
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 header 。例如:
data = SM_TEMPLATE
headers = {
'Content-Type': 'application/soap+xml; charset=utf-8'
}
req = urllib2.Request(site, data, headers)
关于python - 使用 urllib2 进行 SOAP POST,但我不断收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5207208/