Understanding REST and REST API
Services are self-contained unit of software which performs specific task. Various architectural styles exist which are created to guide the design and development of the architecture for World Wide Web. REST is one of them which is a software architectural style which defines constraint on creation of web services.
Today we look more in detail about REST (Representational State Transfer), its features, constraints etc.
REST is an architectural style. It is described as a set of formal and informal guidelines to create architecture – also known as ‘Constraints’. It was introduced and defined in year 2000 by Roy Fielding during his research in PHD program. REST is used to describe web architecture to identify problems and compare alternatives solutions to ensure that protocols should not violate the core constraint which make web successful.
Constraints of REST architecture
Uniform Interface – This defines interface between client and server. It simplifies and decompose the architecture to enable every part to be developed. It has four guiding principles:
- Resource based – individual resources defined using a unique resource identifier known as URI. The resources are different from representations sent back to customers. Server represents some database records expressed to HTML, XML or JSON based on server request and details of implementation.
- Resources manipulation by representation – Information on servers to be modified or deleted while client represent a resource associated to metadata.
- Self-descriptive messages – message contains all details to indicate how it is processed
- Engine of Hypermedia Application State (HATEOAS) – State by response code, response headers and body content
Stateless – request itself contains state required to handle the request. The URI defines the resource and the state of the resource. The in between state is not maintained, updated, or communicated during the session.
Client-Server – this separates client from the server. Servers and clients are independently placed and developed until there is a change in interface.
Layered System – Directly connected to end server or by some intermediately which client can’t tell. The scalability is improved using intermediary servers by enabling load balancing and providing shared cache.
Cacheable – On World Wide Web customer responses are cached. Responses clearly define themselves unacceptable to prevent clients from reuse of stale or inappropriate data for further requests.
Code on demand (Optional) – servers temporarily moves or optimize functionality of client by logic in execution. Examples are Java applets and client-side scripts.
Features of ‘REST’
- REST is an architectural style
- REST uses SOAP web services
- REST uses URI to represent business logic
- REST uses less bandwidth
- RESTFUL services inherit security measures from underlying transport protocol
- REST permits different data formats such as plain text, HTML, XML, JSON etc.
- REST is platform and language independent
- can easily be used in the presence of firewalls
Pros and Cons of ‘REST’
- Lightweight and simple interface (URI based)
- Uses HTTP service methods
- Can leverage cache to obtain better performance
- Less dependency on tools
- Human readable
- Easy to understand and learn
- Ability to organize complicated applications and makes it easy to use resources
- Point to point communication
- Tied to HTTP
- Lack of standards
- Security relies on HTTP authentication
- Lack of state
- Lacks metadata standard and tools support
What is REST API?
As we all know that an Application Program Interface (API) is a code which allows communication between two machines or two software. An API basically accepts a request from application and returns the desired data as response. APIs usually work in REST style and hence also called RESTful API or REST API.
How does a REST API work?
The key components of REST API model are:
Client: It is a software running on the device of the user and is responsible for initiating conversation.
Server: It offers an API as a means of access to its data
Resource: It is any kind of information that can be provide to the client by server.
The client sends HTTP REQUEST to get access, and the server generates HTTP RESPONSE with encoded data on resource. Both the REST messages (request and response) contain information on how to interpret and process them and are thus self-explanatory.