Search code examples
pythonpysimplesoap

How can I add header to SOAP with pysimplesoap?


I have this scheme:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="tns" xmlns:nam="https://iotchet.ru/namespases">
   <soapenv:Header>
      <tns:RequestHeader>
         <!--Optional:-->
         <tns:sessionkey>b01daba7289e4e8baa87dbd1eb8c4f6b</tns:sessionkey>
      </tns:RequestHeader>
   </soapenv:Header>
   <soapenv:Body>
      <tns:SendContainer>
         <!--Optional:-->
         <tns:Container>
            <nam:name>test</nam:name>
            <nam:content></nam:content>
         </tns:Container>
      </tns:SendContainer>
   </soapenv:Body>
</soapenv:Envelope>

Here I make request:

from pysimplesoap.client import SoapClient

client = SoapClient(wsdl='http://localhost:5555/api/containerize?wsdl')
client.SendContainer(Container={'name': 'test', 'content': 'test'})

I can't find the way to add the header to my client request.


Solution

  • For example, assume you want to add Credential in Header as follows.

    <soapenv:Header>
      <Credential>
         <user>hoge</user>
         <password>hoge</password>
      </Credential>
    </soapenv:Header>
    

    Then you can use the following code.

    client = SoapClient(wsdl=WSDL_FILE)
    client['Credential'] = { 'user': 'hoge', 'password': 'hoge' }
    

    This sort of rule is hard to find because pysimplesoap is abandoned and its documents are lost.

    I found this in issues_test.py in GitHub. Because this program is testing features taken from the real cases, it has useful examples.

    If you are willing to switch, I recommend Zeep. It has more features and much nicer documents.