Saturday, July 12, 2014

SOAP ( Simple Object Access Protocol )





In this post I'm going through SOAP web service with a quick introduction and basics about SOAP.


What is a web service ?

Web services are services which are used to communicate over HTTP(Hyper Text Transfer Protocol). These web services are used to communicate between different programs in different platforms which are developed by using different languages. So Web services are very important now a days.

What is SOAP ?

  • Simple Object Access Protocol (Protocol is a set of rules & regulations).
  • SOAP is a communication protocol.
  • The base of SOAP is; make data available as services. 
  • SOAP is based only on XML.
  • This is platform / language independent.
  • SOAP is designed for data communication via internet.

Image resource - http://feather.elektrum.org/

Versions of SOAP

  • 1.1 version (default version)
  • 1.2 version (current version)

Why SOAP 1.2 ?

  • SOAP version 1.2 is much faster that 1.1 version.
  • Cleaner than 1.1 version
  • Better web integration
  • More versatile (More about SOAP 1.2)

SOAP Message

SOAP message is simply like a ordinary XML document. It consist of four parts.
  1. Envelope (mandatory) - Define the scope of the SOAP message
  2. Header (optional) - Optional attributes used in processing the message
  3. Body (mandatory) - XML data
  4. Fault (optional) - Optional fault element provide information about errors
You can understand about the syntax from following example code. It is just like a envelop with a letter.

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
...
</soap:Header>

<soap:Body>
...
  <soap:Fault>
  ...
  </soap:Fault>
</soap:Body>

</soap:Envelope>

xmlns:soap Namespace

This should be definitely http://www.w3.org/2001/12/soap-envelope 

soap:encodingStyle

This attribute defines the data type which is used in the document


Example SOAP message

SOAP request 

POST /InStock HTTP/1.1
Host: www.website.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.website.com/user">
  <m:GetUsername>
    <m:userName>Ann</m:userName>
  </m:GetUsername>
</soap:Body>

</soap:Envelope>



SOAP Response 


HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.website.com/user">
  <m:GetUsernameResponse>
    <m:surName>Carolyn</m:surName>
  </m:GetUsernameResponse>
</soap:Body>

</soap:Envelope>


Advantages of SOAP

  • Well structured and formal
  • Work with any communication protocol
  • Integrated security and authentication. 

Disadvantages of SOAP

  • SOAP is slow, because it uses XML meta data. Then it gets a lot of time to read them.
  • Uses a lot of bandwidth.
  • You can use XML only

When to use SOAP ?

  • Payment gateways
  • Commercial and financial services
  • Telecommunication services 

SOAP clients

  • PayPal SOAP API