pythonpython-3.xexceptionsoapsuds

Get the http://tempuri.org/ node used in the latest SOAP request when using suds-py3


I am making a SOAP webcall using suds-py3. This issue here is that the namespace for http://tempuri.org/ keeps switching between 'ns0' to'ns1'. So before I pass my xml parameters I want to know which abbrevation it is accepting at the moment 'ns0' or 'ns1'

What I plan is to create an exception deliberately and parse through the exception output to get the abbreviation it is expecting. Below is my code. This gives me proper exception but when I try to get it into a variable it is not helping, it just gives me the class.

from suds import WebFault

c = client.Client(wsdl_url)

try:
  c.service.getcategorylist()
except WebFault:
  x=repr(WebFault)

it prints out the below

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://tempuri.org/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:getcategorylist/>
   </ns1:Body>
</SOAP-ENV:Envelope>

but when i try to check what is in x, it give below

"<class 'suds.WebFault'>"

I need the SOAP request part into a variable, so that I can get out the namespace abbreviation for http://tempuri.org/ 'ns0'

Thanks for help.


Solution

  • Ok ,Have found a solution to this issue.

    wsdl_url = 'https://..?wsdl'
    c = client.Client(wsdl_url)
    
    # call service without any parameters to get error
    try:
        c.service.getcategorylist() 
    except:
        pass
    
    # this will give the last call details
    x = str(c.last_sent())
    

    X will save the last call details as below

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:ns0="http://tempuri.org/" xmlns:SOAP- 
    NV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header/>
       <ns1:Body>
          <ns0:getcategorylist/>
       </ns1:Body>
    </SOAP-ENV:Envelope>
    

    extract the currently used node abbreviation

    ix = x.find('="http://tempuri.org/"')
    node = x[ix - 3:ix]
    

    Now node holds the latest node abbreviation 'ns0/ns1' and I use this throughout the other code to make real service requests