Pages

Saturday, November 23, 2013

SOAP and ReST Service

Start from here

What are soap services? / What is soap service?

Simple Object Access Protocol (SOAP) is a XML based structured protocol specification to be used in the implementation of web service. In simple words SOAP web services uses XML as a mechanism for exchanging of information (one way) between the two programs (end points) over any transport layer protocol.
Though for message negotiation and transmission soap is not relies on any particular transport protocol but HTTP (Hyper Text Transfer Protocol) or SMTP (Simple Mail Transfer Protocol) are the two most popular transport protocols used with SOAP. Similarly SOAP is not relying on any particular programming language or operating system as far as they understand XML and SOAP message.
For example, one program running in Windows operating system such as Windows 7 can communicate to another program running in Linux operating system. For this soap will use XML and HTTP.
SOAP message is consist of three parts, i.e. envelope, header and body as shown in below image.
Simple SOAP Architecture
Simple SOAP Architecture

SOAP:envelope

The root element of SOAP message is a SOAP envelope which states what is in the soap message and how to process it. Application looks at the soap:envelope and easily determines what could be the SOAP message. It also helps application to identify the version of the SOAP being used.

SOAP:header

SOAP:Header is an optional element of the soap message. The element from this part contains application specific information. The information should be related to soap message and could be anything like authentication method or payment methods etc.
Though it is an optional element, but if present then it should be the first child element of the soap:envelope.

SOAP:body

SOAP:body is a mandatory element unlike soap header. This part contains all information related to call and response of the web service. Actual soap message goes here.
The format of soap message is as below –

<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<!-- Header elements. -->
</soap:Header>
<soap:Body>
<!-- Body elements. -->
</soap:Body>
</soap:Envelope>
The most important aspect of the SOAP is that without making any changes to the original SOAP message it can tunnel through firewalls and proxies.

What are restful services? / What is restful service?

REST stands for REpresentational State Transfer is a newcomer in its type of family. REST provides very easy access and implementation to a web service that ultimately aims to work best on the web. Restful web services are an architecture style stateless web services mostly used for building client-server applications over HTTP.
In REST, every resource is addressed or accessed by Unique URI (Uniform Resource Identifier) and data and functionality forms a resource. Representation State Transfer defines some set of operations and resources acts according to these operations.
Instead of using XML for request/response, it relies on simple unique URI and that’s why it is also considered as lightweight alternative to SOAP. As it is rely on URI as a resource identifier, the standard CRUD operations of HTTP (GET, PUT, DELETE, POST, HEAD) can be performed on that URI/resource.
SOAP provides the response in XML format only while REST doesn’t have to use XML only. Though XML and HTML formats are often used for REST response, REST can also response in JASON, CSV and RSS formats. There are various options you can choose for the parsing of the REST response. Each of these responses has their own advantages and disadvantages.
“ReST” is lightweight service alternative to most of the mechanism including SAOP and RPC (Remote Procedure Call) but it has no “W3C” recommendations.
In short, ReST is simple, language & platform independent and Standard HTTP based mechanism that can be used through the Firewalls also.
Following are few simple examples of Get & POST upon ReST –
Example 1 :
  1. Request: (URL – http://abc.com/)
    GET /
    Accept: application/json+userdb
  2. Response :
    200 OK
    Content-Type: application/json+userdb
    {
    "version": "1.0",
    "links": [
    {
    "href": "/category",
    "rel": "list",
    "method": "GET"
    },
    {
    "href": "/category",
    "rel": "create",
    "method": "POST"
    }
    ]
    }
Example 2 :
  1. Request : (URL – http://abc.com/category)
    GET /category
    Accept: application/json+userdb
  2. Response :
    200 OK
    Content-Type: application/json+userdb
    {
    "categories": [
    {
    "categoryid": 1,
    "categoryname": "Mobile",
    "links": [
    {
    "href": "/category/1","rel": "self","method": "GET"
    },
    {
    "href": "/category/1",
    "rel": "edit",
    "method": "PUT"
    },
    {
    "href": "/category/1",
    "rel": "delete",
    "method": "DELETE"
    }
    ]
    },
    {
    "categoryid": 2,
    "categoryname": "Tablet",
    "links": [
    {
    "href": "/category/2",
    "rel": "self",
    "method": "GET"
    },
    {
    "href": "/category/2",
    "rel": "edit",
    "method": "PUT"
    },
    {
    "href": "/category/2",
    "rel": "delete",
    "method": "DELETE"
    }
    ]
    }
    ],
    "links": [
    {
    "href": "/category",
    "rel": "create",
    "method": "POST"
    }
    ]
    }
Example 3:
  1. Request : (URL – http://abc.com/category)
    POST /category
    Accept: application/json+userdb
    Content-Type: application/json+userdb
    {
    "categoryname": "Laptops"
    }
  2. Response :
    201 Created
    Content-Type: application/json+userdb
    {
    "category": {
    "categoryid": 3,
    "categoryname": "Laptops",
    "links": [
    {
    "href": "/category/3",
    "rel": "self",
    "method": "GET"
    },
    {
    "href": "/category/3",
    "rel": "edit",
    "method": "PUT"
    },
    {
    "href": "/category/3",
    "rel": "delete",
    "method": "DELETE"
    }
    ]
    },
    "links": {
    "href": "/category",
    "rel": "list",
    "method": "GET"
    }
    }
I hope this articles helps you to understand the difference between SOAP and ReST.

Also