Written by Sümeyye Sever (notes I took while creating web development projects)
REST (Representational State Transfer) is an architectural style for designing networked applications. It provides a set of principles and constraints to create efficient, scalable, and interoperable web services. REST is not a protocol or standard but a guideline that developers follow to build APIs.
A RESTful API is an implementation of REST principles in the form of a web service. It allows clients (such as web browsers, mobile apps, or other servers) to interact with resources on a server via HTTP methods. RESTful APIs are a practical application of the REST architectural style.
Key Concepts of REST
- Resource-Based:
- Everything in REST revolves around resources.
- A resource can be any object or data, such as a user, a product, or a document.
- Each resource is identified by a URI (Uniform Resource Identifier).
- Example:
https://api.example.com/users refers to a list of users.
- Representation of Resources:
- Resources are represented in different formats like JSON, XML, HTML or plain text.
- JSON is the most commonly used format because it is lightweight and easily readable.
- Stateless Communication:
- Each RESTful interaction between the client and server is stateless.
- The server does not remember the client’s state between requests. Each request must include all the information the server needs to fulfill it.
- This makes REST highly scalable, as the server doesn't need to maintain session data.
- Uniform Interface:
- RESTful systems use a consistent, predictable, and uniform approach to interacting with resources.
- Operations are performed using standard HTTP methods:
- GET: Retrieve a resource.
- POST: Create a new resource.
- PUT: Update a resource entirely.
- PATCH: Update part of a resource.
- DELETE: Remove a resource.
- Client-Server Separation:
- REST separates the client (e.g., frontend) and server (e.g., backend) responsibilities.
- The client handles the user interface and interacts with the REST API.
- The server manages the data and business logic.
- Layered System:
- REST allows for a layered architecture where components (e.g., caching systems, load balancers) can exist between the client and server without affecting the interaction.
How REST Works
1. Client Request:
- The client (e.g., a web app or mobile app) sends an HTTP request to the server.
- The request includes:
- URL: Identifies the resource (e.g.,
/users).
- HTTP Method: Specifies the action (e.g.,
GET or POST).
- Headers: Include additional information (e.g., content type, authentication).
- Body: Optional, contains data for the request (e.g., new user details in a
POST request).
2. Server Response:
- The server processes the request and sends an HTTP response.
- The response includes:
- Status Code: Indicates success or error (e.g.,
200 OK, 404 Not Found).
- Headers: Provide metadata about the response.
- Body: Contains the resource data or a confirmation of the operation.
REST Example
Let’s take an example of a RESTful API for managing books.