python suds SOAP 请求中错误的命名空间前缀 [英] python suds wrong namespace prefix in SOAP request
问题描述
我使用 python/suds 来实现客户端,但我在发送的 SOAP 标头中为 wsdl 中的 element ref=
定义的特定类型的参数得到了错误的命名空间前缀.
I use python/suds to implement a client and I get wrong namespace prefixes in the sent SOAP header for a spefic type of parameters defined by element ref=
in the wsdl.
.wsdl 正在引用数据类型 .xsd 文件,请参见下文.问题在于函数 GetRecordAttributes
及其类型为 gbt:recordReferences
的第一个参数.
The .wsdl is referencing a data types .xsd file, see below. The issue is with the function GetRecordAttributes
and its first argument of type gbt:recordReferences
.
文件:browse2.wsdl
<xsd:schema targetNamespace="http://www.grantadesign.com/10/10/Browse" xmlns="http://www.grantadesign.com/10/10/Browse" xmlns:gbt="http://www.grantadesign.com/10/10/GrantaBaseTypes" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:import schemaLocation="grantabasetypes2.xsd" namespace="http://www.grantadesign.com/10/10/GrantaBaseTypes"/>
<xsd:element name="GetRecordAttributes">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="gbt:recordReferences">
</xsd:element>
参考文件:grantabasetypes2.xsd
<element name="recordReferences">
<complexType>
<sequence>
<element name="record" minOccurs="0" maxOccurs="unbounded" type="gbt:MIRecordReference"/>
</sequence>
</complexType>
</element>
suds 发送的 SOAP 请求:
<SOAP-ENV:Envelope xmlns:ns0="http://www.grantadesign.com/10/10/GrantaBaseTypes" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://www.grantadesign.com/10/10/Browse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns1:Body>
<ns2:GetRecordAttributes>
<ns2:recordReferences>
<ns0:record>
</ns0:record>
</ns2:recordReferences>
</ns2:GetRecordAttributes>
</ns1:Body>
</SOAP-ENV:Envelope>
问题 :
有错误的前缀,应该是
因为它属于命名空间...GrantaBaseTypes
在 .xsd 中定义.
Problem : <ns2:recordReferences>
has wrong prefix, should be <ns0:recordReferences>
since it belongs to the namespace ...GrantaBaseTypes
defined in the .xsd.
这发生在 wsdl 中由 ref=
定义的所有参数上.如何自动修复此问题?
This happens for all arguments defined by ref=
in the wsdl. How can this be automatically fixed?
注意:我通过 curl 手动发送 xml SOAP 请求,检查了good"前缀是否被服务接受.
Note: I checked that the "good" prefix is accepted by the service by manually sending the xml SOAP request via curl.
更新
我干预了 SUDS 源代码,以下经验性修复强制所有具有 ref=
属性的元素采用 ref-ed 命名空间(以前,它们采用架构根命名空间或任何 tns
是):
I meddled with SUDS source code and the following empirical fix forces all elements with ref=
attribute to assume the ref-ed namespace (previously, they take on the schema root namespace or whatever tns
is):
文件:/suds/xsd/sxbase.py
File: /suds/xsd/sxbase.py
class SchemaObject(object):
....
def namespace(self, prefix=None):
ns = self.schema.tns
#FIX BEGIN
if self.ref and self.ref in self.schema.elements.keys():
ns = self.ref
#FIX END
适用于我的服务,但我不确定它是否会破坏其他东西.我更喜欢不更改 SUDS 源代码的更智能的解决方案.
Works with my service, but I'm not sure if it'll break other things. I would prefer a smarter solution that does not change SUDS source code.
谢谢,
亚历克斯
推荐答案
编写一个 Suds 插件来修改发送之前的 XML.
Write a Suds plugin to modify the XML before it is sent.
from suds.client import Client
from suds.plugin import MessagePlugin
class MyPlugin(MessagePlugin):
def marshalled(self, context):
#modify this line to reliably find the "recordReferences" element
context.envelope[1][0][0].setPrefix('ns0')
client = Client(WSDL_URL, plugins=[MyPlugin()])
引用 Suds 文档:
Quoting Suds documentation:
编组()
为插件提供在发送之前检查/修改信封文档的机会.
marshalled()
Provides the plugin with the opportunity to inspect/modify the envelope Document before it is sent.
这篇关于python suds SOAP 请求中错误的命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!