RESTful WebServices
Nowadays, people new to Software Developement hear the term REST Service or REST API very often but still couldn't ask or get a deep insight into what they exactly are. Let's cover that here.
Normally two computers (usually one is client and other is server) communicate using HTTP protocol where client sends a request in Browser to server and Server responds with some data embedded in HTML Page. If client itself is a server for other clients, and it wants to use the data of the response from the first server, it can't utilise it from HTML Page. So Here comes web services which allows server to send just data. The web services can be either REST services which we will talk about or SOAP Web services, which are not so popular nowadays and aren't discussed here.
Restful Webservice is a service which is built on the REST architecture. REST stands for Representational State Transfer.
Note: Api and Rest are used interchangeably. API stands for Application Program Interface.
In RESTful webservices, anything that we can manipulate with a URL is a Resource.
RESTful webservices relies on HTTP methods to manipulate a resource on the server.
- GET - To retrieve a resource from the server
- POST - To create a resource on the server
- PUT - To change the state or to update the resource
- DELETE - To remove or delete a resource from the server
Following are the most important elements of any REST Service:
REQUEST HEADERS
It includes some additional information like content type, authorization or authentication.
REQUEST BODY
This is where you send data required. For example, to insert a record in database with POST method.
RESPONSE BODY
It constitutes the response data, which can be used. For example, student details returned in JSON format.
RESPONSE STATUS CODES
These specify the status of operation performed. Example,
200 OK - Successful
201 Created - Created
401 Unauthorised - Invalid authentication
Principles of REST
In Rest, you do not need to follow any standards which gives a lot of flexibility. While there are some principles which needs to be followed. It helps to leverage the benefits of REST.
Following are the principles of REST through which a client interacts with a REST Server:
- Stateless - Server should not retain any client specific information with it. Every request must be treated as a new request and client has to provide all the information.
- Cache - Client will cache the response and so if the name request is sent again, the request doesnt has to go to server.
- Layered System - It gives flexibility to distribute the service in to multiple layers. Client doesn't need to take care of it.
- Uniform Contract - Identification of Resources through URL, Resource manipulation through Representation (JSON or XML), Self Descriptive message, Hypermedia as the Engine of Application State (HATEOAS) which means additional information in response to client so that client can take further action.
- Client-Server - Server will not care of presentation. Client wont care of data storage.
REST is an Architectural Style and not a protocol that has defined a flexible way to communicate data between two machines.
Comments
Post a Comment