What is Restful Webservice ?
Table of Content
- What’s a webservice ?
- Types of webservices
- Characteristics of RESTful webservice
- Benefits of RESTful web service
- Designing RESTful URIs
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.
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
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.
Requirement | HTTP Method | URI Template | URI |
---|---|---|---|
List all departments | GET | library.com/departments | library.com/departments |
Lookup a particular department | GET | library.com/departments/{id} | library.com/departments/1 |
List all books in a department | GET | library.com/departments/{id}/books | library.com/departments/1/books |
Lookup a particular book | GET | library.com/departments/{id}/books/{book-id} library.com/books/{book-id} | library.com/departments/1/books/23 library.com/books/23 |
Create a new department | POST | library.com/departments [body] | library.com/departments |
Update an existing department | PUT | library.com/departments/{id} [body] | library.com/departments/1 |
Delete a department | DELETE | library.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.