让我先说我对 SOAP 以及事情应该如何工作仍然很陌生。我正在使用垂直响应 API。
我在获取 suds 以正确构造请求的 xml 时遇到问题。这是一些代码:
from suds.client import Client
url = 'https://api.verticalresponse.com/wsdl/1.0/VRAPI.wsdl'
client = Client(url)
vr = client.service
...
test_list = (
(
{
'name' : 'email_address',
'value' : login['username'],
},
{
'name' : 'First_Name',
'value' : 'VR_User',
}
),
(
{
'name' : 'email_address',
'value' : 'api-support@verticalresponse.com',
},
{
'name' : 'First_Name',
'value' : login['username'],
},
),
)
# sid and cid are correctly retrieved prior to this point
print "Sending test message..."
vr.sendEmailCampaignTest({
'session_id' : sid,
'campaign_id' : cid,
'recipients' : test_list,
})
在这种情况下 login['username'] 只是一个电子邮件地址。
该代码引发此错误:
suds.WebFault: Server raised fault: 'Application failed during request deserialization: Too many elements in array. 4 instead of claimed 2 (2)
这里是 sendEmailCampaignTest 的定义:http: //developers.verticalresponse.com/api/soap/methods/campaigns/sendemailcampaigntest/
这是记录输出的 xml。 此 XML 不正确且不起作用(我删除了 session_id 和 list_id 以便在此处显示):
<?xml version="1.0" encoding="utf-8" ?>
DEBUG:suds.client:headers = {'SOAPAction': u'"VR/API/1_0#sendEmailCampaignTest"', 'Content-Type': 'text/xml; charset=utf-8'}
ERROR:suds.client:<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns3="http://api.verticalresponse.com/1.0/VRAPI.xsd" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="VR/API/1_0" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns4:sendEmailCampaignTest>
<args xsi:type="ns3:sendEmailCampaignTestArgs">
<session_id xsi:type="ns1:string">redacted</session_id>
<campaign_id xsi:type="ns1:int">redacted</campaign_id>
<recipients xsi:type="ns3:ArrayOfNVDictionary" ns2:arrayType="ns3:NVDictionary[2]">
<item>
<name xsi:type="ns1:string">email_address</name>
<value xsi:type="ns1:string">email@example.com</value>
</item>
<item>
<name xsi:type="ns1:string">First_Name</name>
<value xsi:type="ns1:string">VR_User</value>
</item>
<item>
<name xsi:type="ns1:string">email_address</name>
<value xsi:type="ns1:string">api-support@verticalresponse.com</value>
</item>
<item>
<name xsi:type="ns1:string">First_Name</name>
<value xsi:type="ns1:string">email@example.com</value>
</item>
</recipients>
</args>
</ns4:sendEmailCampaignTest>
</ns0:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:http failed:
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode><faultstring>Application failed during request deserialization: Too many elements in array. 4 instead of claimed 2 (2)
</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
我正在基于以下 ruby 脚本(由 Vertical Response 提供)移植我的脚本:
test_list = [
[{
'name' => 'email_address',
'value' => $username
},
{
'name' => 'First_Name',
'value' => 'VR_User'
}
],
[{
'name' => 'email_address',
'value' => 'api-support@verticalresponse.com'
},
{
'name' => 'First_Name',
'value' => $username
}
]
]
print "Sending test message...\n"
temp0 = vr.sendEmailCampaignTest({
'session_id' => sid,
'campaign_id' => cid,
'recipients' => test_list,
})
它输出以下 xml。 这个 XML 是正确的并且可以工作(我删除了 session_id 和 list_id):
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<n1:sendEmailCampaignTest xmlns:n1="VR/API/1_0"
env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<args xmlns:n2="http://api.verticalresponse.com/1.0/VRAPI.xsd"
xsi:type="n2:sendEmailCampaignTestArgs">
<session_id xsi:type="xsd:string">redacted</session_id>
<campaign_id xsi:type="xsd:int">redacted</campaign_id>
<recipients xmlns:n3="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="n3:Array"
n3:arrayType="n2:NVDictionary[2]">
<item xsi:type="n3:Array"
n3:arrayType="n2:NVPair[2]">
<item>
<name xsi:type="xsd:string">email_address</name>
<value href="#id9496430"></value>
</item>
<item>
<name xsi:type="xsd:string">First_Name</name>
<value xsi:type="xsd:string">VR_User</value>
</item>
</item>
<item xsi:type="n3:Array"
n3:arrayType="n2:NVPair[2]">
<item>
<name xsi:type="xsd:string">email_address</name>
<value xsi:type="xsd:string">api-support@verticalresponse.com</value>
</item>
<item>
<name xsi:type="xsd:string">First_Name</name>
<value href="#id9496430"></value>
</item>
</item>
</recipients>
</args>
</n1:sendEmailCampaignTest>
<value id="id9496430"
xsi:type="xsd:string"
env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">address@example.com</value>
</env:Body>
</env:Envelope>
我了解错误在于收件人的构造。它应该包含两个项目,每个项目包含两个项目,但我使用 suds 的 python 脚本将其设置为包含四个未嵌套的项目。所以我的问题是我怎样才能得到正确构造 xml 的泡沫?
编辑:我尝试过的:
>>> url = 'https://api.verticalresponse.com/wsdl/1.0/VRAPI.wsdl'
>>> client = Client(url)
>>> args = client.factory.create('ns0:sendEmailCampaignTestArgs')
>>> args
(sendEmailCampaignTestArgs){
session_id = None
campaign_id = None
recipients =
(ArrayOfNVDictionary){
_arrayType = ""
_offset = ""
_id = ""
_href = ""
_arrayType = ""
}
}
在我看来,suds 没有正确构造 ArrayOfNVDictionary(它没有可以保存 NVDictionary 的实际数组元素),任何有助于确定你会很棒。