DEV Community

Om Vaja
Om Vaja

Posted on

REST Api

What is REST API?

REST stands for Representational State Transfer. To, understand REST Api we have to understand the meaning of Representational and State Transfer words in REST Api.

  • Representational: At the end of the day, the client and server send or receive some data. These data or resources are represented in a format such as JSON, XML, or HTML. For example, if a client requests information about a user, the server might return a JSON representation of the user's data.

  • State Transfer: When a client requests a resource from the server, the server sends the current state of that resource. The current state of the resource means up-to-date data. The client can then modify that resource by sending updates, and the server will save the new state.

REST API Constraints

It is an architecture pattern for designing network applications. It's helpful to increase scalability, performance, and maintainability.

  1. Client-Server Architecture:
    Client-server architecture means the client sends the request to the server and the server responds to this request. The client is responsible for the user interface and the server is responsible for application logic and data handling.

  2. Stateless:
    There are two types of communication. 1. Stateless and 2. stateful

  • Stateless means the server is not storing any information about the client or any previous requests. Each request is treated as an independent request.

  • Stateful means the server stores the information about the client across multiple requests.

REST Api should be stateless means each request from the client to the server must contain all the necessary information to understand and process the request. If the client wants to get user data then the get request will be sent and the client has to send the user id with the request if the client wants to update the user data then a put or patch request will be sent and again client has to send the user id with the request. so basically, each request is independent no data sharing should be there between requests.

3.Cacheability:
Responses can include information to tell the client whether the response can be cached and for how long, making repeated data access more efficient.

When the server sends a response, it can include headers like:

-Cache-Control: max-age=600: This tells the client that the response can be cached for 600 seconds (10 minutes).

-ETag: "abc123": This is a unique identifier for the version of the resource. The client can use this to check if the data has changed before asking the server again.

4.Layered System:
The architecture can consist of multiple layers (such as security, caching, and load balancing) between the client and server. Each layer operates independently, unaware of other layers.

A proxy or load balancer might be placed between the client and the server to distribute incoming requests without the client being aware of it.

5.Uniform Interface:
Uniform Interface means there are some standardized way for communication between the client and the server.

There are four principles:

  1. Resource Identification:
    Each resource should be identified uniquely by a URI, such as '/users/123' or '/products/44'.

  2. Manipulation of Resources Through Representations:
    Clients send representations of resources when they want to create or modify them, and the server responds with representations when they retrieve or request resources.

REST uses standard HTTP methods (like GET, POST, PUT, DELETE) to perform actions on resources.

GET: Retrieve data.
POST: Create new data.
PUT: Update existing data.
DELETE: Remove data.

3.Self-descriptive Message:
Every message between the client and server contains all the information to understand what’s happening. This includes status codes like 200 OK or 404 Not Found and the format like JSON or XML.

4.Hypermedia as the Engine of Application State(HATEOAS):
HATEOAS means including links in the response. The server helps the client know what to do next by including links in the response.

Just like if you want to see the followers, follow this link.

If you fetch the GitHub response you will see the following response.

Image description

and if you check one of the user data then you will see links that guide the user if he wants to check followers or any other information.

Image description

In short, REST APIs are a powerful way to structure communication between clients and servers. By following principles like statelessness, and uniform interfaces, and features like HATEOAS, you can design scalable and flexible systems.

Thank You.

Top comments (0)