Tuesday, November 19, 2013

SOAP and REST

What is SOAP?
SOAP stands for Simple Object Access Protocol. SOAP is a XML-based protocol for communication between applications. SOAP is platform and language independent. SOAP can be used over any transport protocol, such as HTTP, SMTP, TCP, or JMS.
The SOAP architecture consists of several layers of specifications for message format, message exchange patterns, transport protocol bindings, message processing models, and protocol extensibility.
A SOAP message is a XML document containing an Envelope that defines what is in the message and how to process it, a Header that contains header information, and a Body that contains call and response information.
Below is an example to show a typical SOAP operation. A GetUserDetails request is sent to a server. The request has a UserID parameter. The server returns a PhoneNumber parameter. The result is embedded inside a SOAP XML response Envelope and can be integrated into an application. The namespace for the function is defined in http://www.example.org/phonebook.
The SOAP request could be:
 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 pb="http://www.example.com/phonebook">
  <pb:GetUserDetails>
   <pb:UserID>386</pb:UserID>
  </pb:GetUserDetails>
 </soap:Body>
</soap:Envelope>
The SOAP response could be:
 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 pb="http://www.example.com/phonebook">
  <pb:GetUserDetailsResponse>
   <pb:PhoneNumber>7893652459</pb:PhoneNumber>
  </pb:GetUserDetailsResponse>
 </soap:Body>
</soap:Envelope>
What is REST?
REST stands for Representational State Transfer. REST is not a protocol but an architectural style for designing network applications. REST is also platform and language independent. It is a lightweight alternative to SOAP and WSDL-based Web Services. Unlike SOAP, REST is not bound to XML. CSV and JSON can also be used.  REST doesn’t have standard specifications. It defines a set of architectural principles based on resources. REST uses URI and HTTP for methods GET (retrieve data), POST (update), PUT (add) and DELETE to expose resources.
A REST request for the above example could be:
http://www.example.com/phonebook/UserDetails/386
The response could be a plain text with raw value “7893652459” and is not embedded inside anything.
In REST, URIs is the equivalent of nouns. In the example, the URI method is not called GetUserDetails, but UserDetails. It is a common convention in REST design to use nouns rather than verbs to represent resources.
The example above is a simple REST request with a single parameter. REST can easily handle more complex requests, including multiple parameters. In most cases, you'll just pass along parameters in the URI. For example:
http://www.example.com/phonebook/UserDetails?firstName=Lisha&lastName=Li
Summary
In summary, both SOAP and REST are used for communication between applications. They are both platform and language independent. SOAP uses interfaces and named spaces to expose operations, while REST uses URI and HTTP methods to expose resources. REST doesn’t enforce message format, while SOAP only permits XML. SOAP has strict specification for every part of implementation, while REST gives the concept and is less restrictive about the implementation. In addition, REST follows stateless model, while SOAP has specifications for stateful implementation.

No comments: