How to create a soap header using the pudon suds library?

I need to call a SOAP service with this message:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="https://secure.xpslogic.com/service/wijnen/sub"> <soapenv:Header> <sub:auth> <token>?</token> <!--Optional:--> <user_id>?</user_id> <!--Optional:--> <user_token>?</user_token> </sub:auth> </soapenv:Header> <soapenv:Body> <sub:customer_logos_pull> <!--Optional:--> <language>?</language> <!--Optional:--> <limit>?</limit> <!--Optional:--> <options_utc>?</options_utc> </sub:customer_logos_pull> </soapenv:Body> </soapenv:Envelope> 

I have php example code that sets the headers as follows (and which works fine):

auth = array (); $ auth ['token'] = 'xxx'; if ($ auth) {// add auth header $ This-> clients [$ module] → __ setSoapHeaders (new SoapHeader ($ Namespaces, 'Auth', $ Auth)); }

Now I build the (empty) body and header as follows Python suds lib :

 from suds.client import Client from suds import WebFault client = Client(url='https://example.com/sub.wsdl') auth = client.factory.create('auth') auth.token = 'xxx' client.set_options(soapheaders=auth) customerLogosPull = client.factory.create('customer_logos_pull') result = client.service.customer_logos_pull(customerLogosPull) 

but that gives me the message not well-formed (invalid token) . When you enable registration, I consider this a message:

 DEBUG:suds.mx.core:processing: (Content){ tag = "auth" value = (auth){ token = "xxx" user_id = None user_token = None } type = <Element:0x10ff8c950 name="auth"> <Complex:0x10ff8cbd0> <Sequence:0x10ff8cc50> <Element:0x10ff8cd10 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> <Element:0x10ff8cd50 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> <Element:0x10ff8cd90 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> </Sequence> </Complex> </Element> } 

It looks pretty good, but it also gives not well-formed (invalid token) . Seeing that suds docs has 3 examples on how to go in the soap headers, I also tried the other two:

 >>> token = client.factory.create('auth.token') >>> token.set(TOKEN) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: token instance has no attribute 'set' 

and

 >>> client.set_options(soapheaders={'auth': {'token': 'xxx'}}) >>> customerLogosPull = client.factory.create('customer_logos_pull') >>> result = client.service.customer_logos_pull(customerLogosPull) 

which gives this content in the logs, and still a not well-formed (invalid token) :

 (Content){ tag = "auth" value = { token = "xxx" } type = <Element:0x106049290 name="auth"> <Complex:0x106049510> <Sequence:0x106049590> <Element:0x106049650 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> <Element:0x106049690 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> <Element:0x1060496d0 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> </Sequence> </Complex> </Element> } 

Does anyone know how I can correctly set the token in the header using Python? All tips are welcome!

+1
source share
1 answer

I got the title “Soap,” as shown below:

 from suds.sax.element import Element ssnp = Element("xsi:SessionHeader").append(Element('xsi:sessionId').setText("xxxxxxx")) client.set_options(soapheaders=ssnp) 

Soap-relevant Headers shown below in xml soap request

 <SOAP-ENV:Header> <xsi:SessionHeader> <xsi:sessionId>xxxxxxx</xsi:sessionId> </xsi:SessionHeader> </SOAP-ENV:Header> 

We can see which request was sent using print client.last_sent()

+2
source

Source: https://habr.com/ru/post/988340/


All Articles