Open In App

REST API Architectural Constraints

REST stands for REpresentational State Transfer and API stands for Application Program Interface. REST is a software architectural style that defines the set of rules to be used for creating web services. Web services which follow the REST architectural style are known as RESTful web services. It allows requesting systems to access and manipulate web resources by using a uniform and predefined set of rules. Interaction in REST based systems happen through Internet’s Hypertext Transfer Protocol (HTTP). 

A Restful system consists of a:



It is important to create REST API according to industry standards which results in ease of development and increase client adoption. 

Architectural Constraints of RESTful API: There are six architectural constraints which makes any web service are listed below:



The only optional constraint of REST architecture is code on demand. If a service violates any other constraint, it cannot strictly be referred to as RESTful. 

Uniform Interface: It is a key constraint that differentiate between a REST API and Non-REST API. It suggests that there should be an uniform way of interacting with a given server irrespective of device or type of application (website, mobile app). 

There are four guidelines principle of Uniform Interface are:

Stateless: It means that the necessary state to handle the request is contained within the request itself and server would not store anything related to the session. In REST, the client must include all information for the server to fulfill the request whether as a part of query params, headers or URI. Statelessness enables greater availability since the server does not have to maintain, update or communicate that session state. There is a drawback when the client need to send too much data to the server so it reduces the scope of network optimization and requires more bandwidth.

Cacheable: Every response should include whether the response is cacheable or not and for how much duration responses can be cached at the client side. Client will return the data from its cache for any subsequent request and there would be no need to send the request again to the server. A well-managed caching partially or completely eliminates some client–server interactions, further improving availability and performance. But sometime there are chances that user may receive stale data. 

Client-Server: REST application should have a client-server architecture. A Client is someone who is requesting resources and are not concerned with data storage, which remains internal to each server, and server is someone who holds the resources and are not concerned with the user interface or user state. They can evolve independently. Client doesn’t need to know anything about business logic and server doesn’t need to know anything about frontend UI. 

Layered system: An application architecture needs to be composed of multiple layers. Each layer doesn’t know any thing about any layer other than that of immediate layer and there can be lot of intermediate servers between client and the end server. Intermediary servers may improve system availability by enabling load-balancing and by providing shared caches. 

Code on demand: It is an optional feature. According to this, servers can also provide executable code to the client. The examples of code on demand may include the compiled components such as Java Servlets and Server-Side Scripts such as JavaScript. 

Rules of REST API: There are certain rules which should be kept in mind while creating REST API endpoints.

URI HTTP verb Description
api/users GET Get all users
api/users/new GET Show form for adding new user
api/users POST Add a user
api/users/1 PUT Update a user with id = 1
api/users/1/edit GET Show edit form for user with id = 1
api/users/1 DELETE Delete a user with id = 1
api/users/1 GET Get a user with id = 1

Note : You can easily use GET and POST but in order to use PUT and DELETE you will need to install method override. You can do this by following below code :

npm install method-override --save

This simply require this package in your code by writing :

var methodOverride = require("method-override");

Now you can easily use PUT and DELETE routes :

app.use(methodOverride("_method"));

HTTP verbs: Some of the common HTTP methods/verbs are described below:

Article Tags :