Rest stands for representational state transfer.
The term “state transfer” in REST refers to the transfer of state between client and server.
Means the current state of the system or server can be shared with client and can be updated upon client request.
Lets understand With example, Suppose my backend manages the list of students, and there are 100 students in this list.
so the current state is that we have list of 100 students and when clients request the list of 100 students it is shared with client.
when client sends the request of adding a new student to the system, it is handled by backend and list is updated.
and same if client wants to delete the student.
hence, state transfer is taking place between the client and the server.
RESTful systems follow a set of principles that define how resources are presented and transferred over a network. Here are the key principles of REST:
- Client-Server Architecture: The system is divided into client and server components, which are independent of each other and can evolve separately. This separation of concerns improves scalability and simplifies components.
- Statelessness: Each request from a client to the server must contain all the information necessary to understand the request, and the server must not store any client context between requests. This simplifies server design, improves scalability, and reliability.
- Uniform Interface: The interface between client and server should be uniform, meaning that the same methods (HTTP verbs) and status codes should be used consistently across all resources. This simplifies the architecture and promotes decoupling between client and server.
- Resource-Based: Resources are the key abstraction in RESTful systems, and each resource should be uniquely identifiable through a URI (Uniform Resource Identifier). Resources are manipulated using a limited set of well-defined operations (HTTP verbs).
- Representation: Resources can have multiple representations, such as JSON, XML, HTML, or others. Clients and servers can negotiate the representation format based on their preferences or the capabilities of the client or server.
- Stateful Interactions through Hypermedia: RESTful systems may use hypermedia as the engine of application state (HATEOAS), meaning that clients interact with the server by following hypermedia links contained within resource representations. This allows for a more flexible and discoverable API.
- Cacheability: Responses from the server should be explicitly labeled as cacheable or non-cacheable to improve performance and scalability. Clients can then use caching mechanisms to store responses and reuse them when appropriate.
- Code on Demand (optional): Code on demand is an optional constraint in REST, meaning that servers can optionally provide executable code (e.g., JavaScript) to be executed by the client as part of the application logic. THowever, it is often not utilized in practice due to security concerns and the complexity it adds to client-server interactions.
- Layered System: RESTful systems often use a layered architecture, where each component (client, server, intermediary) interacts only with the layer directly beneath it, and not with any other layers. This helps in creating a modular and scalable architecture, where components can be added, modified, or removed without affecting other parts of the system.