Understanding REST API (Representational State Transfer API)
A REST API is an interface that follows the principles of REST, an architectural style designed to take full advantage of the existing protocols of the Web. It focuses on identifying resources by names and transferring their states.
1. The Three Elements of REST API
A REST API is defined by three core components:
- Resource - URI: Every resource is identified by a unique Uniform Resource Identifier (URI).
- Example:
/users/123(User with ID 123)
- Example:
- Verb - HTTP Methods: Defines the action to be performed on the resource.
- GET: Retrieve data (Read)
- POST: Create new data (Create)
- PUT/PATCH: Update existing data (Update)
- DELETE: Remove data (Delete)
- Representation: The format in which the server returns data to the client, most commonly JSON or XML.
2. Key Characteristics of REST
- Stateless: The server does not store any client context (sessions) between requests. Each request must contain all the information necessary to understand it.
- Cacheable: Since it relies on the HTTP protocol, it can utilize standard caching mechanisms to improve performance.
- Layered System: A client cannot ordinarily tell whether it is connected directly to the end server or to an intermediary like a proxy or load balancer.
- Uniform Interface: By applying a software engineering principle of generality to the component interface, the overall system architecture is simplified.
3. What is "RESTful" Design?
An API is called RESTful when it strictly adheres to the constraints of the REST architecture.
* Bad Example: GET /get_user_info/123 (Includes a verb in the URI)
* RESTful Example: GET /users/123 (Identifies the resource with a noun and expresses the action via the HTTP method)