Intro to REST APIs
Tags: Crud Operations, Rest Api Architecture, Rest Api, Api Security, Rest Api Design

What is REST?

REST stands for Representational State Transfer. It’s an architectural style that defines a set of recommendations for designing loosely coupled applications that use the HTTP protocol for data transmission.

In loosely coupled applications, components are independent, and changes made in one component will not affect the operation of others.

REST APIs follow REST architectural style for creating web services. REST APIs are sometimes called as RESTful APIs because they implement REST architectural style.

REST doesn’t prescribe how to implement the principles at a lower level. Instead, the REST guidelines allow developers to implement the details according to their own needs.

APIs that use HTTP protocol are called as ‘web services’. REST APIs work only on HTTP Protocol. Web services built following the REST architectural style are called RESTful web services.

To create a REST API, you need to follow six architectural constraints:

Uniform Interface Client-server Separation Stateless Cacheable Resources Layered System Code on Demand

Lets look at them one by one.

Uniform Interface: It suggests that there should be an uniform way of interacting with a given server irrespective of device or type of application (website, web app or mobile app).

Uniform Interface has the following guideline principles:

Resource Based

Manipulation of Resources Through Representations

Self-descriptive Messages

Hypermedia as the Engine of Application State (HATEOAS)

Resource-Based

Every content in the web application can be considered as a resource, it can be in the form of text files, html pages , images, videos etc. Individual resources are identified through the request itself .

A resource has an identifier, which is a URI that uniquely identifies that resource. For example, This URI contains the post details of id=1 :

https://my-json-server.typicode.com/typicode/demo/posts/1



When this URI is accessed via a browser, it sends a GET request to the server.

The Response is received in JSON format:

{“id”:1,”title”:”Post 1"}

Manipulation of Resources Through Representations

Consider a case where the user has the permission to create or delete a resource.



Now, To create or delete a resource, a request from the client must contain all the necessary information including the metadata to create or delete the resource on the server.

Example: To create a new post with an id “4”, For url: https://my-json-server.typicode.com

POST /typicode/demo/posts?id=4 /HTTP 1.1

Host: my-json-server.typicode.com

content-type: application/json; charset=utf-8 title= “rigyaja”

Self-descriptive Messages

Each message includes enough information to describe how to process the message so that server can easily analyses the request.

Clients and Servers interact with each other using messages. When a client makes a request. The server sends a response.

Every request has a header and a body, header contain metadata that carries required information and other information, while the body contains the message itself.

Example:

Let’s say there is a dog image in jpg and webp format. Image in jpg format:

We used Postman Application to send the request.

The same resource can be represented in webp format:

Image in webp format

Hypermedia as the Engine of Application State (HATEOAS)

It is used to include links for each response so that client can discover other resources easily.

The term “hypermedia” refers to any content that contains links to other forms of media such as images, movies, and text.

When hypermedia links are added in a response, It allows the client to dynamically navigate to the appropriate resources by traversing the hypermedia links.

Example: Let us consider a REST API Endpoint, When a request is sent by the client, the response contains the hyper links and the methods to access all the resources within the /wp/v2/ path.



Here, GET can be used to access “/wp/v2/” path and similarly GET, POST methods can be used to access “/wp/v2/posts” path.

2. Client-server separation — The client and the server should act independently. It means the changes made in the client side must not dependent on the server side. The client must be able to change and evolve without having to change anything on the server. The same goes for server.

3. Stateless — Every request sent by the client should contain all the information the server needs to know. This is similar to Manipulation of Resources Through Representations, a principle of uniform Interface which was discussed earlier.

4. Cacheable resources — When a server sends a response to client, it should contain information about whether the data it sends is cacheable or not. These cacheable resources must arrive with a version number so that the client avoids making a request for the same data more than once.

5. Layered system — When a client makes a request for a resource, the request can go via several layers of servers until it reaches the final server that returns the response. These layers must not affect either the request or the response.

6. Code on demand [optional] — When a server sends a response to the client, the response must contain an executable code (e.g., JavaScript within an HTML response) that the client can execute and run.

REST API METHODS

REST APIs methods are mainly based on CRUD operations. CREATE, READ, UPDATE and DELETE.

We have come across major HTTP Methods such as GET, POST, DELETE etc. in our daily lives. So lets look at the most used HTTP Methods, before we try to understand CRUD operations.

GET — It is used to retreive data from a server at the specified resource. For example, when a GET request is made to this URI https://jsonplaceholder.typicode.com/comments, it returns a list of all available comments.

POST — It is used to create or update a resource. Every POST request contains a message in the body section.

For Example,

POST https://my-json-server.typicode.com/typicode/demo/posts

Host: my-json-server.typicode.com

{“id”:4,”title”:”Post 4"}

this operation creates a new post with id =4.

DELETE — It is used to delete a resource.

DELETE https://my-json-server.typicode.com/typicode/demo/posts/4

CRUD Operations

CREATE operation is used to create a resource (POST)

READ operation is used to read resource (GET)

UPDATE operation is used to update a resource (POST)

DELETE operation is used to delete a resource (DELETE)

In Summary,

GET /comments — Retrieves a list of comments

GET /comments/12 — Retrieves a specific comment #12.

POST /comment/13 — Creates a new comment if comment #13 does not exist. Or updates the existing comment.

DELETE /comments/12 — Deletes comment #12.

Feel Free to Connect with us on Telegram and Discord.