SOAP and REST

This post covers features of SOAP and REST and basic differences between them.

SOAP

REST

  • SOAP stands for Simple Object Access Protocol
  • It is an XML based web service
  • Adds a layer on top of the application protocols. HTTP is the widely used application protocol.
  • SOAP is considered heavy weight as it provides significant pre-build extensibility in the form of the WS* standards through additional layer.

1

  • Request includes HTTP header, SOAP Envelope (which includes SOAP Header, SOAP Body (which includes XML message))
  • As it has additional layer, it supports attaching files in the request which should be in base64 bit format.
  • It provides more security than REST.
  • REST stands for Representational State Transfer
  • REST typically supports all types of text data formats. Widely used formats are XML and JSON
  • Uses HTTP protocol only. No another layer on top of HTTP protocol.
  • REST is light weight as it leverages HTTP transport and uses all HTTP Action Verbs (POST, GET, PUT, DELETE)

2

 

  • Request includes just HTTP header and XML/JSON message.
  • It doesn’t support attachment of files in the request.
  • REST provides security like token based, but comparatively REST security is less than SOAP.

Sample SOAP Request:

A SOAP Message is encoded as an XML document, consisting of an <Envelope> element, which contains an optional <Header> element, and a mandatory <Body> element. The <Fault> element is sub element of Body is used for reporting errors.

5.png

 

SOAP Envelope with SOAP Header.

3

Sample SOAP Response:

6.PNG

Sample REST Request in XML:

7

Sample REST Response in XML:

8

Sample REST Request in JSON:

9

Testing of SOAP/REST web service Requests can be done using SOAP UI tool.

Advertisements

Invoking REST webservice from JQuery using an AJAX call

Below code helps you to understand how we can Invoke a REST web service on click of a button using JQuery and AJAX.

Note: In Java you can Invoke REST web service using ApacheHTTPClient library.

HTML File:

<!DOCTYPE html>
<html>
<head>
<sscript type=”text/javascript”
src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js”></script&gt;
<sscript type=”text/javascript”>
$(document).ready(function () {
$(“#mybutton”).click(function () {
alert(‘welcome’);

$.ajax( {
type : “GET”, url : “http://services.groupkt.com/country/get/iso2code/IN&#8221;, dataType : “json”, success : function (data) {
alert(“Reading Msg: ” + data.RestResponse.messages);
alert(“Reading Name: ” + data.RestResponse.result.name);
}
});

});

});
</script>
</head>
<body>
<button type=”button” id=”mybutton”>Invoke REST WS</button>
</body>
</html>

Sample REST Services (thanks for making them available to developers for testing purpose):

http://www.groupkt.com/post/c9b0ccb9/country-and-other-related-rest-webservices.htm
http://services.groupkt.com/country/get/iso2code/IN
http://services.groupkt.com/country/get/all
http://rest-service.guides.spring.io/greeting

REST_invoke