What is Restful Webservice ?

...

Table of Content

What’s a webservice ?

The term webservice is a service offered by an electronic device to another electronic device, communicating with each other via world wide web.

In other words, services that are exposed to the internet for programatic access are called web service.

webservice

Types of webservices

  • RESTful webservice – Representation State Transfer
  • SOAP webservice – Simple Object Access Protocol

Characteristics of RESTful webservice

Representation state transfer (REST) was found by Roy Fielding from the University of California. It is a very simplified and lightweight webservice compared to SOAP. Performance, scalability and portability are the main principles behind the REST design.

Client-Server Based Architecture

REST follows a client-server based architecture. It’s a computing model in which the server hosts, delivers and manages the resources and services. However, the client(mobile/desktop/other programs) are consumer of these resources and services. In practice this type of architecture has one or more clients connected to the hosting server over a network or internet connection.

Stateless

This is the most important characteristic of a RESTful service. Statelessness is a fundamental aspect of the modern internet – so much so that every single day, you use a variety of stateless services and application. A REST request consists of all the data that is needed for the server to process the request and give a response. Once a request is served, its state is not maintained by the server. However, every request is independent of each other. Hence making it a stateless architecture.

Cache

Caching refers to storing the server response in the client itself. In order to scale an application well, we need to cache content and deliver it as response.

Resource Based URIs

Unlike SOAP( Action Based ), REST follows resource based URIs. A resource can be considered as an object in an application. Lets take an example of a weather website to understand this better:

Action based: weather-app.com/weatherLookup.do?zipcode=123456

Resource based: weather-app.com/zipcodes/123456

In the above example, area zip code is considered as a resource and 123456 uniquely identifies an area.

HTTP – Hyper Text Transfer Protocol

REST follows HTTP protocol for communication between the client and server. It uses the HTTP methods – GET, POST, PUT, DELETE etc to inform the server about the action to be performed on a resource. Let’s again consider the above example of a weather website. The resource based URI mentioned above has information only about the resource we want to access, but not the action to be performed. However, looking at the action based URI, we can get both the resource details and the action (lookup) to be performed on the resource.

In REST, this is solved using the standard HTTP methods. Therefore, the correct representation of the above action based URI in REST will be as follows.

HTTP GET -> weather-app.com/zipcodes/123456

Benefits of RESTful web service

  • Modern and most popularly used
  • Light weight
  • Follows HTTP protocol
  • Supports multiple formats – TEXT, XML, JSON
  • Could be easily accessed from JQuery or JavaScript

Designing RESTful URIs

rest-design

Now let’s get our hands dirty and start designing some RESTful URIs. For this part, we will consider building URIs for a library application. To keep it simple, this library will have only two resources, department and book. There can be multiple departments in the library and there can be one-or-many books in a department.

RequirementHTTP MethodURI TemplateURI
List all departmentsGETlibrary.com/departmentslibrary.com/departments
Lookup a particular departmentGETlibrary.com/departments/{id}library.com/departments/1
List all books in a departmentGETlibrary.com/departments/{id}/bookslibrary.com/departments/1/books
Lookup a particular bookGETlibrary.com/departments/{id}/books/{book-id} library.com/books/{book-id}library.com/departments/1/books/23 library.com/books/23
Create a new departmentPOSTlibrary.com/departments [body]library.com/departments
Update an existing departmentPUTlibrary.com/departments/{id} [body]library.com/departments/1
Delete a departmentDELETElibrary.com/departments/{id}library.com/departments/1

[body] – It’s a placeholder for request body, which can hold a JSON or XML with information about the resource.

comments powered by Disqus