Search code examples
pythonweb-servicessoapwsdlsoappy

Why Python omits attribute in the SOAP message?


I have a web service that returns following type:

<xsd:complexType name="TaggerResponse">
    <xsd:sequence>
        <xsd:element name="msg" type="xsd:string"></xsd:element>
    </xsd:sequence>
    <xsd:attribute name="status" type="tns:Status"></xsd:attribute>
</xsd:complexType>

The type contains one element (msg) and one attribute (status).

To communicate with the web service I use SOAPpy library. Below is a sample result return by the web service (SOAP message):

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SOAP-ENV:TagResponse>
         <parameters status="2">
            <msg>text</msg>
         </parameters>
      </SOAP-ENV:TagResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Python parses this message as:

<SOAPpy.Types.structType parameters at 157796908>: {'msg': 'text'}

As you can see the attribute is lost. What should I do to get the value of "status"?


Solution

  • The response example you posted (the actual XML coming back from the WS request) does not have the value in it you are looking for! I would suggest this is why SOAPpy cannot return it to you.

    If it is a case of making your code have consistent behaviour in cases where the value is returned and when it isn't then try using dict's get() method to get the value:

    attribute_value = result.get("attribute", None)
    

    Then you can test the result for none. You can also do so like this:

    if not "attribute" in result:
        ...handle case where there is no attribute value...