📰 What is SOAP Api ?

📅 December 5th, 2020 ⏲️ 2 mins 12 secs

An entreprise that would to connect to other entreprise’s service must know the address of the service and then send a request, the server will take this request and then do a job with data received and then send a response.

When a client wants to invoque a service, it’s hard to tell him what data he needs to send and what URL his must specify, and that’s why the client needs a manual guide.

For example, you want to make a call and you don’t know the number by looking at a printed telephone directory. So imagine a web service, all you need is to send him a person’s name and he will send you the number, great huh? But we need to know where is this service located.

The developer of the service must publish a xml file, called WSDL (Web Services Description Language). This file contains all functions, parameters and methods. Cool, now we have the URL of this service and need to consume this SOAP request.

But What is SOAP ?

When you enter a website’s URL on the navigator you need to specify the protocole (How your computer and the server will communicate). Basically it’s http or https for secure connection. Every request have two parts, the header that contains request options, authentications and the body that contains data like form data, uploaded file or parameters.

With soap it’s SOAP (Simple Object Access Protocol). It also contains two parts, a request header that contains authentication and request options and a SOAP Envelop.

If we want to consume a SOAP service we have to provide a WSDL file, for this example we will use this: http://www.dneonline.com/calculator.asmx?wsdl

The header may be like this: Content-Type: text/xml;charset=UTF-8

Then, we must specify the SOAP action in the header, it’s the method that we will call. And it’s like this: soapAction:’http://empuri.org/Divide’

The SOAP envelop

It’s an XML file, also called a payload. For the example of dividing we have to specify a payload like this:

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/"> 
<soapenv:Header/> 
  <soapenv:Body> 
    <tem:Divide> 
      <tem:intA>1</tem:intA> 
      <tem:intB>0</tem:intB> 
    </tem:Divide> 
  </soapenv:Body> 
</soapenv:Envelope>

If you want to test this, you should use SOAP UI, a perfect tool to test SOAP api.

But you can also test it using your terminal with curl (you need to have curl installed)

Here I send a divided 1/0 that causes a Fault error on the server 😁

Source