What is a REST API? (Gemini)
The REST Representational State Transfer architectural style is a set of rules for building web services that focus on resources and the use of standard HTTP methods.
REST was designed to leverage the existing functionality of the HTTP protocol to perform fundamental data management.
REST's primary methods GET, POST, PUT, and DELETE are mapped to four CRUD operations.
The difference between a REST API and other APIs is that REST API isn't a protocol, but a set of guidelines that promote simplicity, scalability, and statelessness.
APIs, like SOAP or gRPC, are typically rigid and protocol-driven each with its own specific rules and formats.
REST API
Representational: In what manner a server sends data to a client.
Instead of transferring a current, live resource, the server sends a representation of that resource's current state.
The representation is a snapshot of the resource at that moment and can be in various formats like JSON or XML.
For example, when you request a user's profile, the server sends a JSON object that represents the user's data (name, email, etc.)
State: Refers to the current state of a resource on the server.
The client's interaction with the server involves "transferring" the state of a resource. For instance, to update a user's profile, the client sends a representation of the new state of the profile to the server.
Transfer: Refers to the act of a client and server exchanging representations of resources.
The client requests a resource, and the server transfers a representation of it. To make changes, the client transfers a new representation of the resource to the server.
Principles of the REST API
Client-Server Architecture: The client for example a web browser or mobile app and the server are independent of each other. The client sends a request to the server, and the server sends back a response.
Stateless: Each request from a client to a server must contain all the information needed to process it. The server doesn't store any information about the client's state between requests. This feature makes the system more scalable.
Uniform Interface: The API uses a consistent way to interact with resources relying on standard HTTP methods to perform actions on resources:
REST was designed to leverage the existing functionality of the HTTP protocol to perform fundamental data management tasks in a logical, resource-oriented manner.
The use of HTTP methods to express actions on a resource (like a user, a product, or a blog post) makes the API intuitive and easy to use.
A Breakdown of the HTTP Methods used in REST.
POST Creates new data. This method is used to submit data to the server, typically creating a new resource. For example, submitting a form to create a new user account would use a POST request.
GET Retrieves data. This method is used to read or retrieve a representation of a resource from a specified URI. It is considered "safe" because it won't change the state of the server. You'd use this to get a product's details or a list of users.
PUT Updates existing data. This method is used to update an entire existing resource with new data provided in the request body. It is "idempotent," meaning that making the same PUT request multiple times will have the same effect as making it once. It completely replaces the old resource with the new one.
DELETE Deletes data. This method is used to remove a specified resource from the server. Like PUT, it's also idempotent; deleting a resource that has already been deleted results in the same final state.
REST APIs often use JSON (JavaScript Object Notation) or XML for data transfer because these formats are lightweight and easy to read