简体   繁体   中英

python suds Error in XML

i tryied for a couple of hours to receive data from an Soap Webservice.

This is my Code:

from suds.client import Client
from suds import WebFault

WSDL_URL = 'gatewaywebservice.asmx?wsdl'
client = Client(WSDL_URL)

checkIfExists = client.factory.create('checkIfExists')
checkIfExists.SessionID = ''
checkIfExists.UserID = 'ttester@email.com'
try:
   response = client.service.CustomerService(checkIfExists)
   #print response
   if response.Error:
      print response.Error
   else:
      pass
except WebFault, e:
  print e
print client.last_sent()
print client.last_received()

This is what i sent:

<SOAP-ENV:Envelope xmlns:ns0="asdf"                                                xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:CustomerService>
         <ns0:CustomerService>
            <ns0:SessionID></ns0:SessionID>
            <ns0:UserID>ttester@email.com</ns0:UserID>
         </ns0:CustomerService>
      </ns0:CustomerService>
   </ns1:Body>
</SOAP-ENV:Envelope>

and this is what the Webserver expect:

<?xml version="1.0" encoding="UTF-8"?>
<GATEWAY xmlns="urn:Software-com:Gateway:v7-00">
<CustomerService>
    <checkIfExists>
        <SessionID/>
        <UserID>test</UserID>
    </checkIfExists>
</CustomerService>
</GATEWAY>

How can I update my code to send an valid request?

Thanks in advance

You can implement a Suds plugin to modify the XML before it is sent.

In the example below the <SOAP-ENV:Envelope> tag is modified to match the webserver expectations:

from suds.client import Client
from suds.plugin import MessagePlugin

class MyPlugin(MessagePlugin):
    def marshalled(self, context):
        envelope = context.envelope
        envelope.name = 'GATEWAY'
        envelope.setPrefix(None)
        envelope.nsprefixes = {'xmlns' : 'urn:Software-com:Gateway:v7-00'}
        # and so on...
        # envelope[0] is the Header tag, envelope[1] the Body tag
        # you can use "print context.envelope" to view the modified XML

client = Client(WSDL_URL, plugins=[MyPlugin()])

You have to complete the marshalled method to fully transform the XML. But before doing that, check if you have the correct WSDL file, as @Martijn Pieters says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM