Reputation: 11
I'm trying to call the Country capitals SOAP API to get the country capital on Python.
import requests
url = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL"
headers = {'content-type': 'text/xml'}
body = """<soapenv:Envelope xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/ xmlns:web=http://www.oorsprong.org/websamples.countryinfo>
<soapenv:Header/>
<soapenv:Body>
<web:CapitalCity>
<web:sCountryISOCode>USA</web:sCountryISOCode>
</web:CapitalCity>
</soapenv:Body>
</soapenv:Envelope>"""
response = requests.post(url,data=body,headers=headers)
print(response.content)
I get a 200 OK response but it doesn't return the actual country capital, instead it just returns the entire content from the browser when you visit this http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
It works without issues on SoapUI, do you know what I'm doing wrong on python?
Upvotes: 1
Views: 5148
Reputation: 77337
A good SOAP client should build the XML for you. SOAP is a transport, like any of a dozen other Remote Procedure Call systems. One SOAP client is zeep. It reads the WSDL and builds a local client for you to call. You can get the job done with
import zeep
url = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL"
client = zeep.Client(url)
print(client.service.CapitalCity("USA"))
Let zeep do the heavy lifting.
Upvotes: 4
Reputation: 2263
The URL address of the service WSDL is: http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL That's why you get the WSDL content instead of the response message.
The URL address of the service endpoint is specified within the WSDL and is: http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso
If you utilize a SOAP library like zeep, you may initialize the client with the WSDL URL, however if you consume the SOAP service on your own (making an HTTP POST request), you need to use the endpoint address.
Upvotes: 0
Reputation:
This works:-
import requests
xml = """<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CapitalCity xmlns="http://www.oorsprong.org/websamples.countryinfo">
<sCountryISOCode>USA</sCountryISOCode>
</CapitalCity>
</soap12:Body>
</soap12:Envelope>"""
url = 'http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso'
headers = {'Content-Type': 'application/soap+xml; charset=utf-8'}
with requests.Session() as session:
r = session.post(url, headers=headers, data=xml)
r.raise_for_status()
print(r.text)
Upvotes: 1