Snake_Byte #16: REST-assured with SOAP and Python

It's easy to use Python and REST with SOAP, without having to cleanout the tools cupboard. Once you have the WSDL and/or a sample request, you're all set. To keep it simple, I used SoapUI to generate a sample request from this WSDL. Using named placeholders, the request can be saved and the elements in the SOAP envelope can be updated at runtime.

Using the Requests library, generate a request to the SOAP endpoint with the request string as the data. Note "GetCitiesByCountry" in the request string. This will be used by the web service, to invoke the correct operation.

Take a look at the WSDL for the namespace and elements that need to be parsed out. Using the built-in xml libraries, we find what we need ... in this case names of cities in the United States, from which I can get weather information.

For some of our solutions, we do utilize other libraries (lxml) and generate heavier-weight objects in accordance to xml schemas. However, for a vast majority of our back-end-integrations, the endpoints/wsdls/namespaces do not change and we just need to apply a "strategy pattern" for updating the elements in the SOAP body.

As you can see, python can be used to make REST-ful requests to "basic" SOAP endpoints. Even more complicated soap endpoints (like MTOM messages with binary attachments) are possible with python and REST. Look for a future snake_byte covering this topic.


For more information about the "tools" used in this snake_byte, here are some useful links.

Requests (

SoapUI (