问题
I'm new to Python and I use suds for wsdl client. How can I create custom request header for this.
XML get from SOAP UI :
<soapenv:Header>
<sbus:SBusContext message-tracking-id="?" correlation-id="?" entry-dtime="?" timestamp="?" entry-system="?" entry-system-principal="?" entry-url="?" priority="?">
<!--Optional:-->
<sbus:Keys>
<!--1 or more repetitions:-->
<sbus:Key keyType="?" keyValDataType="string">
<sbus:KeyValue>?</sbus:KeyValue>
</sbus:Key>
</sbus:Keys>
<!--Optional:-->
<sbus:ExtContext>?</sbus:ExtContext>
</sbus:SBusContext>
</soapenv:Header>
回答1:
EDIT -
I realized you're probably asking about a SOAP header, not an http header. If so, ignore my answer. My bad.
Take a look here: How to add http headers in suds 0.3.6?
You can add the header when creating the client like this:
client = suds.client.Client(url, headers={'key': 'value'})
Or after the client is created, by using set_options like this:
client.set_options(headers={'key2': 'value'})
Worth noting that the original suds package is no longer maintained (last release Sept 2010). It lacks various features you might want, like gzip compression (so don't bother adding an 'accept-encoding:gzip' header if you're using the old suds package). Various forks have sprung up. I believe the most active of them is suds-jurko.
回答2:
This snippet from official SUDS documentation:
from suds.sax.element import Element
client = client(url)
ssnns = ('ssn', 'http://namespaces/sessionid')
ssn = Element('SessionID', ns=ssnns).setText('123')
client.set_options(soapheaders=ssn)
result = client.service.addPerson(person)
回答3:
i needed to add 2 soap headers with xmlns. i found the solution following the answer of @piotr sz. here is the solution as i needed it:
userName = Element('UserName').setText(fco.user)
password = Element('Password').setText(fco.pwd)
fdxns = Attribute('xmlns', "http://fdx.co.il/Authentication")
for field in userName, password:
field.append(fdxns)
client.set_options(soapheaders=(userName, password))
来源:https://stackoverflow.com/questions/22742139/suds-custom-header