【发布时间】:2020-01-08 20:36:53
【问题描述】:
ServiceNow SOAP API 的 WSDL 文件将 request_payload 定义为字符串,而它实际上是一个字典。因此,每当我查询服务时,都会出现错误:
"ValueError: The String type doesn't accept collections as value".
WSDL 文件部分:
<wsdl:definitions targetNamespace="http://www.service-now.com/ChangeOperation" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.service-now.com/ChangeOperation" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<wsdl:types>
<xsd:schema elementFormDefault="qualified" targetNamespace="http://www.service-now.com/ChangeOperation">
<xsd:element name="changeOperationRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="source_system" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="source_uid" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="request_type" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="request_payload" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
使用 SOAPUI 的成功 SOAP 请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.service-now.com/ChangeOperation">
<soapenv:Header/>
<soapenv:Body>
<ns0:changeOperationRequest>
<!--Optional:-->
<ns0:source_system>Script</ns0:source_system>
<!--Optional:-->
<ns0:source_uid>131318</ns0:source_uid>
<!--Optional:-->
<ns0:request_type>getChangeRequests</ns0:request_type>
<!--Optional:-->
<ns0:request_payload>
<start_date>2020-01-08T00:00:00</start_date>
<status_list>Proposed</status_list>
<owning_stream>IB IT</owning_stream>
<full_details>no</full_details>
<result_limit>100</result_limit>
</ns0:request_payload>
</ns0:changeOperationRequest>
</soapenv:Body>
</soapenv:Envelope>
是否可以覆盖从 WSDL 文件中读取的数据类型,或者是否有其他方法可以强制 Zeep 将字段作为字符串发送?
我试过解压字典:
xml = client.service.changeOperationRequest(**request_dict)
并设置关键字参数并且仅将 request_payload 设置为字典,但会导致相同的错误:
xml = client.create_message(client.service, 'changeOperationRequest', source_system='Script',source_uid='131318',request_type='getChangeRequests',request_payload=dictionary)
即使简单地将 request_payload 设置为生成的 xml 也不起作用,因为 xml 标记会扩展。虽然我宁愿不必走手动创建 xml 的路线,但这似乎有点违背了使用 Zeep 的意义。
xml = client.create_message(client.service, 'changeOperationRequest', source_system='EQ CAB Report Script',source_uid='131318',request_type='getChangeRequests',request_payload='<start_date>2020-01-07T00:00:00</start_date><status_list>Proposed</status_list><owning_stream>IB IT</owning_stream><full_details>no</full_details><result_limit>100</result_limit>')
XML 输出:
<sn0:request_payload><start_date>2020-01-07T00:00:00</start_date><status_list>Proposed</status_list><owning_stream>IB IT</owning_stream><full_details>no</full_details><result_limit>100</result_limit></ubs:request_payload>
【问题讨论】: