SUDS Custom Header
Asked Answered
H

4

1

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>
Homework answered 30/3, 2014 at 9:0 Comment(0)
Z
3

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.

Zealotry answered 30/4, 2014 at 18:17 Comment(0)
U
1

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)
Understood answered 10/7, 2017 at 9:58 Comment(1)
hello piotr, seeking for the same answer, i find your answer the only one working of the many showed in the documentation. as my solution required 2 headers, i will also post an answer. all my best.Facture
F
0

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))
Facture answered 18/5, 2020 at 7:55 Comment(0)
B
0

There is a builtin security feature in suds to set the securityheader

from suds.wsse import Security, UsernameToken
url = 'http://something.some/service.svc?wsdl'
client= Client(url, faults=False)
security = Security()
token = UsernameToken('username', 'password')
token.setcreated()
security.tokens.append(token)
client.set_options(wsse=security)
Bulgar answered 30/10, 2023 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.