Introduction
Early in the course of my software development career, I realized much later that I had been writing what would qualify to be REST API, or would qualify to be so with minimal refinement or enhancement.
I had before written so many endpoints in the backend of web-applications, and AJAX/Fetch calls in Javascript for consuming the endpoint resources (by making HTTP requests and receiving HTTP responses). Note: I have never used Axios for making requests, but that shouldn't matter anyway.
Obviously, the concept of HTTP request/response would be applied in all the situations I have gone through as highlighted in the preceding paragraph. In hindsight, I believe that such experience should give anyone the competence to write REST APIs.
Unfortunately, whenever I looked up tutorials or write-ups on REST API (also known as RESTful API), many of them would look so intimidating and confusing instead. I had found it rather difficult to firmly grasp the concept of REST API, until I understood that it is just any other API, but following certain design rules. So I decided today to share my experience just in case it may help others In the same situation where I was back then.
Wait a minute ... up to his point, I have been writing in the "third person". All above was not necessarily my personal story or experience. It was about a few people whom I had interacted with, and I learnt from them that REST API could be challenging to understand. I only represent them by simply summing up important points from their stories. By extension, REST API could be challenging to others too.
Everybody probably knows that REST stands for Representational State Transfer. That is the very easy part – maybe – but what does that mean exactly? This is where the confusion starts.
Definitions usually seem too complicated, unless you directly relate them to practical work that you have done by yourself. This makes sense because theoretical software development effort without a practical touch has very little to yield. Read, read and read – but for as long as you don’t put what you have read into practice, you will gain so little out of your effort, if not nothing at all.
Back to REST API, but let’s start with API.
API (Application Programming Interface) is a software intermediary that allows two applications to talk to each other and share resources (data or functionality). An API offers services to other software to enhance the required functionalities. It can offer resources to external third-party developers. Simple, isn’t it?
The following definition according to IBM makes it clearer: "At the most basic level, an API is a mechanism that enables an application or service to access a
resource within another application or service. The application or
service doing the accessing is called the client, and the application or
service containing the resource is called the server."
An example of API would drive the point home, though it would make more sense if you do develop software yourself.
Take the example of Google Maps. When you search for the location of a place using Google Maps, your request goes to the appropriate endpoint in the Google Maps API. The endpoint processes your request and delivers a response back to you. The response in this case is some data (not HTML), and most likely in form of JSON or XML.
An API uses HTTP methods/verbs (POST, GET, PUT or DELETE) for communication in line with CRUD operations respectively. In general, POST creates a new resource, GET reads a resource, PUT updates a resource, and DELETE deletes a resource.
The concept of "idempotent" is important for REST API as we shall mention later. Just to note: GET, PUT and DELETE methods are idempotent (has no additional effect whether you run it once or repeat it N times). POST method is not idempotent (it can have additional effect of creating N resources if run N times).
To put it another way, GET, HEAD, OPTIONS are safe and idempotent methods whereas PUT and DELETE methods are only idempotent. POST and PATCH methods are neither safe nor idempotent.
An HTTP request consists of method/verb, URI, HTTP version, header, body; whereas an HTTP response consists of response status code, HTTP version, header, body. This is important for the communication.
Now, what is REST API?
REST API is just an API that follows a set of rules or styles for applications and services to communicate with each other using HTTP requests and responses while performing the standard database CRUD operations.
RESTful web services are services that follow REST architecture. REST stands for Representational State Transfer and uses HTTP protocol (web protocol) for implementation. These services are lightweight, provide maintainability, scalability, support communication among multiple applications that are developed using different programming languages. They provide means of accessing resources present at server required for the client via the web browser by means of request headers, request body, response body, status codes, etc.
The REST Server provides access to REST resources (text files, HTML pages, images, or any other dynamic data) whereas the REST client consumes (accesses and modifies) these resources. Every resource is identified globally by means of a URI.
The meaning of idempotent is that even after calling a single request multiple times, the outcome of the request should be the same. While designing REST APIs, we need to keep in mind to develop idempotent APIs. This is because the consumers can write client-side code which can result in duplicate requests intentionally or not. Hence, fault-tolerant APIs need to be designed so that they do not result in erroneous responses.
REST is simply a set of architectural constraints (not a protocol or a standard) that can guide API developers.
REST API must conform to the following constraints/criteria: statelessness, cacheable, decoupled, layered system, and uniform interface. This is where the difficulty in understanding REST API lies.
Therefore, if my API can conform to the above constraints, then I would call it a REST API. This means that the gist of understanding REST API is in the constraints. Let’s try to look at them one by one.
- Statelessness: Each request is independent of the other; a request knows nothing of any other request. REST APIs do not require any server-side sessions. Server applications aren’t allowed to store any data related to a client request.
- Cacheable: Caching helps improve performance and address the constraint of statelessness. When possible, resources should be cacheable on the client or server side
- Decoupled: Client and server are decoupled/detached from each other. The client knows only the URI, and the server passes the response via HTTP without making any client changes.
- Layered : REST APIs should be modular and more flexible to achieve scalability. It should be layered in the way it organizes servers. Requests and responses go through different layers. REST APIs need to be designed so that neither the client nor the server can tell whether it communicates with the end application or an intermediary.
- Uniform Interface: All API requests for the same resource should look the same, no matter where the request comes from. The REST API should ensure that the same piece of data, such as the name or email address of a user, belongs to only one uniform resource identifier (URI).
In addition to the above (and as mentioned earlier), REST APIs are lightweight, easily-maintainable, scalable,
and platform-independent. By now, if you are familiar with HTTP, then most likely understanding REST API has become easier.