问题描述
我想将复杂"标头传递给带有zeep库的SOAP服务
I'd like to pass "Complex" Header to a SOAP service with zeep library
这就是它的样子
<soapenv:Header>
<something:myVar1>FOO</something:myVar1>
<something:myVar2>JAM</something:myVar2>
</soapenv:Header>
我想我以这种方式成功发送了标头
I guess that I succeed in sending a header this way
header = xsd.Element(
'{http://urlofthews}Header',
xsd.ComplexType([
xsd.Element(
'{http://urlofthews}myVar1',
xsd.String()),
xsd.Element(
'{http://urlofthews}myVar2',
xsd.String())
])
)
header_value = header(myVar1='FOO',myVar2='JAM')
print (header_value)
datasoap=client.service.UserRessourcesCatalog(requete,_soapheaders=[header_value])
但是我不知道如何在XSD的标题中声明和传递名称空间"something".
But I don't get how to declare and pass the namespace "something" in my Header with the XSD.
有帮助吗?
提前谢谢.
最好的问候
如文档中所述
http://docs.python-zeep.org/en/master/headers.html
另一种选择是传递一个lxml Element对象.如果wsdl没有定义一个soap头但服务器确实希望它,这通常很有用."
"Another option is to pass an lxml Element object. This is generally useful if the wsdl doesn’t define a soap header but the server does expect it."
这是我的情况,所以我尝试了
which is my case so I tried
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
ET.register_namespace('something', 'http://urlofthews')
headerXML = ET.Element("soapenv:Header")
var1 = ET.SubElement(headerXML, "something:myVar1")
var1.text = "FOO"
var2 = ET.SubElement(headerXML, "something:myVar2")
var2.text = "JAM"
headerDict=xmltodict.parse(ET.tostring(headerXML))
print (json.dumps(headerDict))
datasoap=client.service.UserRessourcesCatalog(requete,_soapheaders=headerDict)
但是我得到了:ComplexType()得到了意外的关键字参数u'soapenv:Header'.签名:``
But I get : ComplexType() got an unexpected keyword argument u'soapenv:Header'. Signature: ``
推荐答案
我最近遇到了这个问题,这是我如何解决的问题.
I recently encountered this problem, and here is how I solved it.
假设您有一个安全"标头,如下所示...
Say you have a 'Security' header that looks as follows...
<env:Header>
<Security>
<UsernameToken>
<Username>__USERNAME__</Username>
<Password>__PWD__</Password>
</UsernameToken>
<ServiceAccessToken>
<AccessLicenseNumber>__KEY__</AccessLicenseNumber>
</ServiceAccessToken>
</Security>
</env:Header>
要在zeep客户端的请求中发送此标头,您需要执行以下操作:
In order to send this header in the zeep client's request, you would need to do the following:
header = zeep.xsd.Element(
'Security',
zeep.xsd.ComplexType([
zeep.xsd.Element(
'UsernameToken',
zeep.xsd.ComplexType([
zeep.xsd.Element('Username',zeep.xsd.String()),
zeep.xsd.Element('Password',zeep.xsd.String()),
])
),
zeep.xsd.Element(
'ServiceAccessToken',
zeep.xsd.ComplexType([
zeep.xsd.Element('AccessLicenseNumber',zeep.xsd.String()),
])
),
])
)
header_value = header(UsernameToken={'Username':'test_user','Password':'testing'},UPSServiceAccessToken={'AccessLicenseNumber':'test_pwd'})
client.service.method_name_goes_here(
_soapheaders=[header_value],#other method data goes here
)
这篇关于Python-Zeep SOAP复杂标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!