1

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?

9
  • I don't know what SoapUI is, but I tried with the python zeep soap client and it worked for me. Running your code I got status 500. Crafting your own SOAP seems like a heavy lift to me. Commented Oct 3, 2021 at 18:01
  • Content-Type needs to be application/soap+xml. Also, remove ?WSDL from the end of your URL Commented Oct 3, 2021 at 18:02
  • @BrutusForcus: Thanks, I did that but its still just returning the full browser data Commented Oct 4, 2021 at 4:50
  • @tdelaney: thanks. I tried using zeep now and now it returns "Country not found in the database". This is how I'm calling it. Is there any error in how i'm passing the xml? client = Client(wsdl_url) result = client.service.CapitalCity(body) Commented Oct 4, 2021 at 4:52
  • Body as in the soap doc above? Just the country name. client.service.CapitalCity("USA"). SOAP is the transport, but its exposed as a regular API at the python level. Commented Oct 4, 2021 at 5:05

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"A good SOAP client should build the XML for you", I cannot highlight this enough; otherwise you shouldn't be using SOAP. Zeep does what is needed.
1

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)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.