Intern
What is REST API?
sobes.tech AI
Answer from AI
REST (Representational State Transfer) is an architectural style for distributed hypermedia systems. It is not a standard or a set of tools, but a set of principles and constraints.
Key principles of REST:
- Client-Server: Clear separation of roles between client and server. The client initiates requests, and the server processes them.
- Stateless: Each request from the client to the server must contain all the information needed for processing. The server does not store the client's state between requests.
- Cacheable: Server responses can be marked as cacheable, allowing clients to reuse previous responses for the same requests, improving performance.
- Layered System: The client does not know whether it is directly connected to the end server or is between it and intermediate servers (such as proxies, load balancers).
- Code-On-Demand: The server can temporarily extend or customize the client's functionality by transferring executable code.
- Uniform Interface: A key principle consisting of four constraints:
- Identification of Resources: Resources (data or objects) are identified using Universal Resource Identifiers (URIs).
- Manipulation of Resources Through Representations: The client interacts with resources through their representations (e.g., JSON, XML). Changing the resource's representation changes the resource itself.
- Self-Descriptive Messages: Each message contains enough information for its interpretation by the recipient, including metadata about the resource and its representation.
- Hypermedia As The Engine Of Application State (HATEOAS): The client, interacting with the server, follows links contained in responses, which determines possible next steps or states of the application.
A RESTful API is an API that adheres to REST principles. In the context of HTTP, RESTful APIs often use standard HTTP methods (GET, POST, PUT, DELETE) to perform operations on resources.
Examples of HTTP methods in RESTful API:
| HTTP Method | Action | CRUD Correspondence |
|---|---|---|
GET |
Retrieve resource(s) | Read |
POST |
Create resource | Create |
PUT |
Update resource | Update |
DELETE |
Delete resource | Delete |
Examples of RESTful API requests:
Getting a list of users:
// GET /users
// This request returns a list of all users.
Creating a new user:
// POST /users
// The request body contains data for the new user.
Getting a specific user by ID:
// GET /users/{id}
// This request returns information about the user with the specified ID.
Updating user information by ID:
// PUT /users/{id}
// The request body contains updated user data.
Deleting a user by ID:
// DELETE /users/{id}
// This request deletes the user with the specified ID.