Understanding SOAP API with WSDL Examples

In this guide, we will explore the concept of SOAP APIs and how WSDL (Web Services Description Language) plays a crucial role in defining the services. You'll find practical examples to help you understand how to implement SOAP APIs effectively.
By Jamie

What is SOAP API?

SOAP (Simple Object Access Protocol) is a protocol used for exchanging structured information in the implementation of web services. It relies on XML to encode its HTTP-based messages.

What is WSDL?

WSDL (Web Services Description Language) is an XML-based language used to describe the services offered by a SOAP API. It provides details on how to interact with the service, including the available methods and data types.

Example SOAP API Using WSDL

To illustrate the usage of a SOAP API with WSDL, let’s consider an example of a simple weather service. Below is a sample WSDL definition for a fictional weather API:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" 
             xmlns:tns="http://example.com/weather" 
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
             xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
             name="WeatherService" 
             targetNamespace="http://example.com/weather">

  <types>
    <xsd:schema targetNamespace="http://example.com/weather">
      <xsd:element name="GetWeatherRequest">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="CityName" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="GetWeatherResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Temperature" type="xsd:float"/>
            <xsd:element name="Condition" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </types>

  <message name="GetWeatherRequestMessage">
    <part name="parameters" element="tns:GetWeatherRequest"/>
  </message>

  <message name="GetWeatherResponseMessage">
    <part name="parameters" element="tns:GetWeatherResponse"/>
  </message>

  <portType name="WeatherServicePortType">
    <operation name="GetWeather">
      <input message="tns:GetWeatherRequestMessage"/>
      <output message="tns:GetWeatherResponseMessage"/>
    </operation>
  </portType>

  <binding name="WeatherServiceBinding" type="tns:WeatherServicePortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="GetWeather">
      <soap:operation soapAction="http://example.com/weather/GetWeather"/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>

  <service name="WeatherService">
    <port name="WeatherServicePort" binding="tns:WeatherServiceBinding">
      <soap:address location="http://api.example.com/weather"/>
    </port>
  </service>
</definitions>

How to Use the Weather SOAP API

Step 1: Create the SOAP Request

To get the weather for a specific city, you would create a SOAP request like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:tns="http://example.com/weather">
  <soap:Header/>
  <soap:Body>
    <tns:GetWeatherRequest>
      <tns:CityName>New York</tns:CityName>
    </tns:GetWeatherRequest>
  </soap:Body>
</soap:Envelope>

Step 2: Sending the Request

You can use various programming languages to send this SOAP request. Below is an example using Python with the requests library:

```python
import requests

url = ‘http://api.example.com/weather’
headers = {’Content-Type’: ‘text/xml; charset=utf-8’}

soap_request = ‘’’