Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a SOAP request using Python requests module

Tags:

python

soap

I used python requests module for REST requests.

I am trying to make a soap request but I wondered couldn’t get an example for this . Here is My soap body and headers.

<auth>
<apikey>xcvzxcvcxzv-a0-0035c6fbc04f</apikey>
</auth>

body

<reports>
<report>
<name>Test</name>
</report>
</reports>

And here is wsdl url

https://ltn.net/webservices/booking/r1/index.wsdl

Please tell me how can I make a post request here using python. If it is not possible using requests module then what could be the other alternatives?

like image 421
masterofdestiny Avatar asked Mar 22 '13 11:03

masterofdestiny


3 Answers

I ran into the same problem recently, and unfortunately neither suds nor jurko-suds(a maintained fork of suds) were able to help. This is mainly because suds kept generating wrongly formatted soap envelope (this especially happens if the soap that is supposed to be generated has some content that's supposed to be inside a CDATA) different from what the server was expecting. This was the case even when I tried injecting the soap envelope myself using the __inject option.

Here's how I solved it using python requests

import requests

request = u"""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://SOME_URL">
   <soapenv:Header>
         <user>{0}</user>
         <pass>{1}</pass>
   </soapenv:Header>
   <soapenv:Body>
      <req:RequestInfo><![CDATA[<?xml version='1.0' encoding='UTF-8'?><request xmlns='http://SOME_OTHER_URL'>
    <Call>
        <callID>{2}</callID>
        ...
    </Call>
    <Foo>
        <Bar>
            <Baz>{3}</Baz>
            ...
        </Bar>
        <Neytri>
            ...
        </Neytri>
    </Foo>
    <Title>{4}</Title>
</request>]]></req:RequestInfo>
   </soapenv:Body>
</soapenv:Envelope>""".format('Jake_Sully', 
                              'super_secret_pandora_password',
                              'TYSGW-Wwhw',
                              'something_cool',
                              'SalaryPayment',
                              'Pandora_title',
                            )

encoded_request = request.encode('utf-8')

headers = {"Host": "http://SOME_URL",
            "Content-Type": "application/soap+xml; charset=UTF-8",
            "Content-Length": str(len(encoded_request)),
            "SOAPAction": "http://SOME_OTHER_URL"}

response = requests.post(url="http://SOME_OTHER_URL",
                     headers = headers,
                     data = encoded_request,
                     verify=False)

print response.content #print response.text

What was really important was to specify the Content-Type in the headers and also the SOAPAction. According to the SOAP 1.1 specifiaction

 The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent. SOAP places no restrictions on the format or specificity of the URI or that it is resolvable. An HTTP client MUST use this header field when issuing a SOAP HTTP Request.       

The value of SOAPAction can usually be found in the wsdl file of the API call that you want to make; if absent from the wsdl file then you can use an empty string as the value of that header

Also see:

  1. http://archive.oreilly.com/pub/post/unraveling_the_mystery_of_soap.html
  2. http://www.prodigyproductionsllc.com/articles/programming/call-a-webservice-using-soap-and-python/
like image 171
Komu Avatar answered Sep 20 '22 23:09

Komu


To work with a SOAP server you should use a specialized library. Unfortunately, there is no good SOAP client modules for Python, the best one I've used is suds.

like image 42
wRAR Avatar answered Sep 16 '22 23:09

wRAR


This is largely hypothetical since I'm not aware of anyone actually having implemented it but suds supports making custom implementations of suds.transport.Transport. Once such is suds.transport.http.HttpTransport. So technically by implementing a transport subclass you could create a suds transport that uses requests.

http://jortel.fedorapeople.org/suds/doc/suds.transport.http.HttpTransport-class.html is what's used by default and it returns http://jortel.fedorapeople.org/suds/doc/suds.transport.Reply-class.html so as you can see, it should be fairly simple to wrap requests replies so that suds understands them. Transport request (that should be sent with requests) is documented here http://jortel.fedorapeople.org/suds/doc/suds.transport.Request-class.html I might sooner or later implement this if no one beets me to it

Why all this effort? Because suds uses urllib2 internally which is far inferior to python-requests as a HTTP client implementation.

Yet one more edit: I made a gist available as a starting point https://gist.github.com/nanonyme/6268358 . It's MIT code and untested but should work as a starting point for the transport.

like image 26
nanonyme Avatar answered Sep 16 '22 23:09

nanonyme