pythonsoapclientsuds

How to set headers properly for suds soap client


I am trying to send a request to an api using suds client.

The request is formed like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v20="https://API">
   <soapenv:Header>
      <v20:apiHeader>
         <v20:username>username</v20:username>
         <v20:password>password</v20:password>
         <!--Optional:-->
         <v20:name>?</v20:name>
      </v20:apiHeader>
   </soapenv:Header>
   <soapenv:Body>
      <v20:getLoginDetails/>
   </soapenv:Body>
</soapenv:Envelope>

I am sending the request for this like:

client = Client('https://wsdl', faults=False)

client.set_options(headers={'username': username 'authToken': auth_token, 'name': ''})
client.service.getLoginDetails()

the error I am receiving is:

(<HTTPStatus.INTERNAL_SERVER_ERROR: 500>, (Fault){
   faultcode = "soap:Server"
   faultstring = "apiHeader should not be null"
   detail = ""
 })

Is this how I should be sending the request? It is definitely something to do with the apiHeader I think; not sure what though, I get the same error using this:

username = Element('username').setText(name)
        password= Element('password').setText(pass)
        header_list = [username, pass]
        self.client.set_options(soapheaders=header_list)
        return self.client.service.getLoginDetails()

Solution

  • I've made this work by using something similar to this:

    from suds.sax.element import Element
    from suds.sax.attribute import Attribute
    code = Element('serviceCode').setText('PABB4BEIJING')
    pwd = Element('servicePwd').setText('QWERTPABB')
    
    reqsoapheader = Element('ReqSOAPHeader').insert(code)
    reqsoap_attribute = Attribute('xmlns', "http://schemas.acme.eu/")
    reqsoapheader.append(reqsoap_attribute)
    
    client.set_options(soapheaders=reqsoapheader)