SOAP API
SOAP (Simple Object Access Protocol) is a messaging protocol that allows programs to communicate with each other using the Internet. It is a standardized protocol that uses XML to encode the messages and HTTP to transport the messages. SOAP is widely used in the integration of web-based systems and is supported by a variety of programming languages.
In this article, we will discuss how to get started with SOAP API. We will cover the following topics:
- What is SOAP API and how does it work?
- Setting up a SOAP API server
- Making SOAP API requests using cURL
- Consuming SOAP API using a programming language
What is SOAP API and how does it work?
SOAP is an XML-based protocol for exchanging information in a decentralized and distributed environment. It allows programs to communicate with each other using the Internet as a medium.
A SOAP message consists of four parts: an envelope, a header, a body, and a fault. The envelope defines the start and end of the message, the header contains optional metadata, the body contains the message payload, and the fault contains error information. Here is an example of a SOAP message:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">
<soap:Header>
<header-element />
</soap:Header>
<soap:Body>
<body-element>
<content>message</content>
</body-element>
</soap:Body>
</soap:Envelope>
A SOAP API is a web service that uses SOAP as the communication protocol. It exposes a set of operations that can be invoked by clients using HTTP POST requests. The request and response are both encoded in XML, and the content of the message is defined using a Web Service Definition Language (WSDL) document.
Setting up a SOAP API server
To set up a SOAP API server, you need to define the operations that you want to expose and the data types that you want to use. You can do this using a WSDL document.
A WSDL document is an XML document that defines the operations and data types of a web service. It consists of four main parts: types, message, portType, and binding. The types element defines the data types used by the web service, the message element defines the input and output messages of the operations, the portType element defines the operations and their messages, and the binding element defines how the operations are mapped to the transport protocol (e.g., HTTP).
Here is an example of a WSDL document:
<definitions xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
xmlns:tns="http://example.com/calculator"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/calculator">
<types>
<xsd:schema>
<xsd:element name="addRequest" type="xsd:int"/>
<xsd:element name="addResponse" type="xsd:int"/>
<xsd:element name="subtractRequest" type="xsd:float"/>
<xsd:element name="subtractResponse" type="xsd:float"/>
</xsd:schema>
</types>
<message name="addRequest">
<part name="arg1" type="xsd:int"/>
<part name="arg2" type="xsd:int"/>
</message>
<message name="addResponse">
<part name="result" type="xsd:int"/>
</message>
<message name="subtractRequest">
<part name="arg1" type="xsd:float"/>
<part name="arg2" type="xsd:float"/>
</message>
<message name="subtractResponse">
<part name="result" type="xsd:float"/>
</message>
<portType name="CalculatorPortType">
<operation name="add">
<input message="tns:addRequest"/>
<output message="tns:addResponse"/>
</operation>
<operation name="subtract">
<input message="tns:subtractRequest"/>
<output message="tns:subtractResponse"/>
</operation>
</portType>
<binding name="CalculatorBinding" type="tns:CalculatorPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="add">
<soap:operation soapAction="http://example.com/calculator#add"/>
<input>
<soap:body use="encoded" namespace="http://example.com/calculator"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="http://example.com/calculator"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="subtract">
<soap:operation soapAction="http://example.com/calculator#subtract"/>
<input>Once you have defined the WSDL document, you can implement the web service using a programming language of your choice. There are several frameworks and libraries available that can help you build a SOAP API server quickly. Some popular options include Apache CXF for Java, Zato for Python, and ASP.NET for .NET.
Get Charlie’s stories in your inbox
Join Medium for free to get updates from this writer.
Once you have implemented the web service, you can deploy it on a web server and make it available to clients over the Internet.
Making SOAP API requests using cURL
cURL is a command-line utility that allows you to make HTTP requests from the terminal. It can be used to make SOAP API requests by specifying the Content-Type and SOAPAction headers and the XML payload in the request body.
Here is an example of a SOAP API request using cURL:
curl -X POST \
https://example.com/soap-api \
-H 'Content-Type: text/xml; charset=utf-8' \
-H 'SOAPAction: "http://example.com/soap-api#operation"' \
-d '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">
<soap:Body>
<operation xmlns="http://example.com/soap-api">
<arg1>value1</arg1>
<arg2>value2</arg2>
</operation>
</soap:Body>
</soap:Envelope>'In this example, we are making a POST request to the https://example.com/soap-api endpoint with the Content-Type header set to text/xml and the SOAPAction header set to the operation defined in the WSDL document. The request body contains the SOAP message encoded in XML.
Consuming SOAP API using a programming language
To consume a SOAP API using a programming language, you need to parse the WSDL document to generate the client-side code. There are several tools available that can do this for you, such as wsdl2java for Java and wsdl2py for Python.
Once you have generated the client-side code, you can use it to invoke the operations exposed by the web service. The client-side code typically provides a method for each operation with the appropriate parameters and return type.
Here is an example of consuming a SOAP API using Python:
from suds.client import Client
from suds.client import Client
client = Client("http://example.com/soap-api?wsdl")
result = client.service.operation(arg1="value1", arg2="value2")
print(result)n this example, we are using the suds library to parse the WSDL document and generate the client-side code. The Client class is used to create a client instance, and the service attribute is used to access the operations exposed by the web service. We can then call the operation() method with the appropriate parameters to invoke the operation and print the result.
Conclusion
In this article, we discussed how to get started with SOAP API. We covered the basics of SOAP and how it works, how to set up a SOAP API server, how to make SOAP API requests, and how to consume a SOAP API using a programming language.
SOAP is a powerful protocol that allows programs to communicate with each other using the Internet. It is widely used in the integration of web-based systems and is supported by a variety of programming languages.
To get started with SOAP API, you need to define the operations and data types of your web service using a WSDL document. You can then implement the web service using a programming language of your choice and deploy it on a web server. To make SOAP API requests, you can use cURL or a programming language. To consume a SOAP API, you need to generate the client-side code from the WSDL document and use it to invoke the operations exposed by the web service.
I hope this article has helped you understand how to get started with SOAP API. If you have any questions or comments, please let me know in the comments section below.
My Fiverr GIG for web development you can order here
Check out my Gig on Fiverr: create web application using react, angular, node, springboot and any framework https://www.fiverr.com/share/69d8wR










