REST API — An Overview of basics

Ashen Malaka
5 min readFeb 2, 2020

Since the invention of the internet, we get much information through the internet. We use different web pages and web applications to obtain that information. Those data come from servers. Through this article, we are looking to get an abstract idea of how a client computer communicates with the servers to obtain those data by REST API.

Why do we need REST API?

Consider an app that needs a lot of input data. Which means the data present in that particular application is not static. This implies the fact that the data present is always changing in this application.

Those mentioned data is received from the server /webserver. So, the client requests the server /webserver for relevant information via an API (Application Program Interface). Then a server/web server sends a response to the request.

The response sent to the client is in the form of an HTML Web Page. But, we would prefer the data to be obtained in the form of a structured format rather than getting a complete HTML page as the response.

Therefore according to the above-mentioned reason and other reasons, the data returned by the server is either in the format of JSON or XML format. Both JSON and XML formats have a hierarchical structure of data.

Format  of  XML  &  JSON
Format of XML & JSON

But, the only issue here is we have to use a lot of methods to get the required information. When we require complex data it is quite difficult to retrieve those data by using these methods.

The solution is the REST APIs.

The REST API creates an object and after that sends the values of an object to the client as the response.

What is the REST API?

Illustration of a REST API

REST creates an object of the data requested by the client and sends the values of the object in response to the user.

So in here, there is an object which is created on the server-side and it is sending the state of that object. this is why REST is known as Representational State Transfer.

REST is an architectural style as well as an approach for communications purposes that are often used in various web services development nowadays.

How a REST API works?

Inside the REST API

Basically the REST API breaks a transaction into small modules. Then, each of these modules is used to address a specific part of the transaction.

This approach provides more flexibility.

But requires a lot of effort to be built from the very scratch.

Features of REST API

01. Simpler than SOAP (SOAP is the mechanism before REST API comes into play).
02. Has proper documentation.
03. Has solutions for various error messages.

Principles of REST API

There are six ground principles laid down by Dr. Fielding who was the one to define the REST API design.

01. Stateless
02. Client-Server
03. Uniform Interface
04. Cacheable
05. Layered system
06. Code on demand

Stateless

This means the communication between the client and the server always contains all the information needed to perform the request.

There is no session state in the server, it is kept entirely on the client’s side.

The request can be either a part of URL, query-string parameters, body, or even headers. The URL is used to uniquely identify the resource and the body holds the state of the requesting resource. Once the server processes the request, a response is sent to the client through body, status or header.

If access to a resource requires authentication, then the client needs to authenticate itself with every request.

Client-Server

The client and the server both have a different set of concerns.

The server stores and/or manipulates information and makes it available to the user in an efficient manner.

The client takes that information and displays it to the user and/or uses it to perform subsequent requests for information.

This separation of concerns allows both the client and the server to evolve independently as it only requires that the interface stays the same.

Uniform interface

This simplifies the architecture, as all components follow the same rules to speak to one another.

It also makes it easier to understand the interactions between the different components of the system.

The following constraints are required to achieve this.

Resource identification

Resource Manipulation using representations

Self-descriptive messages

Hypermedia as the engine of application state

Cacheable

In order to provide a better performance, the applications are often made cacheable.

This is done by labeling the response from the server as cacheable or non-cacheable either implicitly or explicitly.

If the response is defined as cacheable, then the client cache can reuse the response data for equivalent responses in the future.

Layered system

Individual components cannot see beyond the immediate layer with which they are interacting.

This means that a client connecting to an intermediate component, like a proxy, has no knowledge of what lies beyond.

This allows components to be independent and thus easily replaceable or extendable. It also enhances the applications' security as components in each layer cannot interact beyond the next immediate layer they are in.

Code on demand

The code may be downloaded to extend client functionality.

This is an optional constraint and is not used more often.

Methods of REST API

All of us are working with the technology of the web, do CRUD operations.

When we say CRUD operations, that means we create a resource, read a resource, update a resource and delete a resource.

Now, to do these actions, we can actually use the HTTP methods.

Now, we know what is a REST API and what all we need to keep in mind in order to deliver an efficient application.

--

--

Ashen Malaka

Associate Software Engineer @Davton Consulting | Final Year Undergraduate | University of Kelaniya | Traveller