Introduction to REST

Introduction to REST

Introduction

REST stands for Representational State Transfer. It’s a web services access protocol providing standards of communication in the web world. The software systems that follow this style for communication are known as RESTful systems. The other web services access protocols that people use are SOAP, GraphQL, etc.

The main characteristics of a RESTful system compared to others are:

  1. Stateless.
  2. Separate the concerns of the client and server.
  3. Uniform Interface.
  4. Cacheable.
  5. Code-on-demand.
  6. Layered System.

Why choose REST?

The beauty of REST is that it allows you to focus on building app features. You don’t need to break your head thinking client-server interaction, complexity, interoperability, etc.

You can build the client-side and server-side independently. They will communicate over a platform-agnostic format that REST provides. Using the same approach, you can build a modular application. Your modules will implement the REST interface and communicate with each other. This makes your application modular, separate and flexible. Each of your modules has the freedom to grow independently. Here each module provides REST endpoints where other modules interact using REST interfaces.

The other big advantage of RESTful systems is its statelessness. This indicates that the server does not need to worry about the client and vice versa. The request that comes from a client will have all the details to perform a particular operation on the server-side.

In this way, the server doesn’t need to store any previous state or data of the client to operate. This helps RESTful applications to perform quicker, achieve scalability and reliability.

Concept of Resources

REST uses resources rather than commands. A resource can be anything that we can name. It can be a document, an image, a collection of objects, etc. Each resource has a resource identifier for its unique identification during an operation. Resource representation is the state of a resource at any given time. Every resource representation will have an associated media type as well.

RESTful communication

RESTful systems communicate using the REST API (Application Programming Interface). REST APIs work over HTTP and are very light compared to SOAP implementation. There are different HTTP methods available for different operations. REST will leverage these HTTP methods for its operation. The most common methods are: GET, POST, PUT & DELETE.

Every resource liking to interact with a REST interface will have API representation. Let’s take an employee management application. At the server, a list of employees can be represented in REST API format as given below:

             http://www.employeeapp.com/employees

A client application has to first implement a REST client library to do an API request to the server. In Java, we have a JAX-RS library to do so. Now a client-side GET request will be:

           GET http://www.employeeapp.com/employees

For POST, PUT & DELETE operations you will specify the exact resource identifier also.

           DELETE  http://www.employeeapp.com/employees/ID4512

The response can have different formats like CSV, JSON, HTML, etc.

When using JSON, there are JSON formatters and validators are available to check the sanity of the data. JSON Lint (https://jsonlint.com) is an example.

The below given diagram depicts REST service interaction in a client-server environment.

Summary

REST is a communication framework between a web client and a server. There are multiple reasons to choose REST over other such web communication frameworks. It’s scalability, flexibility and wide adoption makes this framework popular in the web services world.