Making a SOAP request using Python requests module
python, soap
Solution
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:
- http://archive.oreilly.com/pub/post/unraveling_the_mystery_of_soap.html
- http://www.prodigyproductionsllc.com/articles/programming/call-a-webservice-using-soap-and-python/
Problem
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?