“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.
Weekly Programming Series - A Web Services Primer
A Web Services Primer (9/10): Until We REST!
Yashavant Kanetkar and Asang Dani
Representational State Transfer (REST)
REST has its origins in a PhD thesis submitted by Roy Fielding – one of the principal authors of the HTTP specification. He proposed a new architectural style of programming in a distributed environment using an elegant and simple approach which relies on two fundamental principles:
- Represent/map the functionality of a Web Service using a URL scheme.
- Implement the APIs using standard HTTP methods, viz: GET, POST, LIST, PUT, DELETE etc.
To make things more concrete, consider the CurrencyWebService implemented using a SOAP approach. It implements the following APIs:
| Function |
Description |
|---|---|
|
CountryCurrencyGetCurrency ( String cname ) |
Returns the currency details for specified country |
|
float GetRateByCountry ( String from, String to ) |
Returns the latest rate of conversion from one country's currency to another |
| float GetRateBySymbol ( String from, String to ) | Returns the latest rate of conversion from one currency symbol to another |
| String [ ] GetCountryNames( ) | Returns an array of country names |
|
CountryCurrency [ ] GetCountryCurrencies( ) |
Returns an array containing names of all countries along with their currency names and symbols |
| Table 1: SOAP based Web Service Model |
In the SOAP based approach, WSDL is used to convey signature of APIs and associated types (e.g. CountryCurrency). If we follow the REST based approach, the URLs to represent corresponding APIs would be as shown in Table 2.
| URL |
Description |
|---|---|
|
http://localhost/CurrencyService/ country/India |
GET http request on this URL will return the currency information for a country (India in our case). |
|
http://localhost/CurrencyService/ country/India/Japan |
GET http request on this URL will return the latest conversion rate from Indian Rupees to Japanese Yen using country name as parameter. |
|
http://localhost/CurrencyService/ country/INR/JPY |
GET http request on this URL will return conversion rate from Indian Rupees to Japanese Yen using respective currency symbols as parameters. |
|
http://localhost/CurrencyService/ country |
LIST http request on this URL will return the list of country names. |
|
http://localhost/CurrencyService/ symbol/ |
LIST http request on this URL will return the list of countries, currency names and symbols. |
| Table 2: REST based Web Service Model |
As you can clearly see, the REST model captures the essence of APIs in URLs and standard HTTP operations like GET, LIST, PUT, DELETE on these URLs. This simplicity is what makes REST so appealing. While SOAP is standards based REST offers freedom to the clients of a Web Service. REST truly captures the essence of HTTP "resources" in URLs (or nouns) and operations (or verbs) on those resources. In the next section, we will show you how to extend the existing Web Service implementation to provide REST based access to the CurrencyWebService.



