Okay, let's talk about 2.3.d RESTful APIs. This builds directly on our understanding of HTTP.
What is an API?
First, a quick reminder: An API (Application Programming Interface) is a set of rules, protocols, and tools that allows different software applications to communicate with each other. It defines how requests should be made and what responses can be expected.
What is REST?
- REST (Representational State Transfer) is an architectural style for designing networked applications, particularly web services. It's not a strict protocol like HTTP, but rather a set of constraints or principles. When followed, these constraints lead to systems that are scalable, performant, reliable, and easy to modify. REST was defined by Roy Fielding in his doctoral dissertation in 2000.
Key Constraints of REST:
Adhering to these constraints makes an API "RESTful":
-
Client-Server Architecture:
- Separates the user interface concerns (client) from the data storage and logic concerns (server).
- This separation allows client and server components to evolve independently, improving portability and scalability.
-
Stateless:
- Each request from the client to the server must contain all the information needed for the server to understand and process the request.
- The server does not store any client context (session state) between requests. Any session state is kept entirely on the client.
- Benefits: Improves visibility (easier monitoring), reliability (easier recovery from failures), and scalability (any server can handle any request).
-
Cacheable:
- Responses must explicitly or implicitly define themselves as cacheable or non-cacheable.
- If a response is cacheable, the client (or an intermediary cache) is given the right to reuse that response data for later, equivalent requests.
- Benefits: Improves network efficiency, perceived performance, and scalability by reducing load on the server. Leverages HTTP caching mechanisms.
-
Uniform Interface: (This is a core principle that simplifies and decouples the architecture)
- Identification of Resources: Individual resources (e.g., a user, a product, an order) are identified using URIs (Uniform Resource Identifiers). Ex:
/users/123
,/products/456
. - Manipulation of Resources Through Representations: Clients interact with resources by exchanging representations of those resources (commonly JSON or XML). The representation contains enough information to modify or delete the resource on the server.
- Self-Descriptive Messages: Each message (request/response) includes enough information to describe how to process it. This often involves using standard HTTP methods (GET, POST, PUT, DELETE) and headers like
Content-Type
andAccept
to define the format of the data. - Hypermedia as the Engine of Application State (HATEOAS): Responses should include links (hypermedia controls) that guide the client on what actions they can perform next. For example, a response for a user resource might include links to "edit-profile" or "view-orders". This allows the API structure to evolve without breaking clients, as clients discover actions dynamically via links rather than hardcoding URIs. (This is the most "mature" and often least strictly implemented constraint).
- Identification of Resources: Individual resources (e.g., a user, a product, an order) are identified using URIs (Uniform Resource Identifiers). Ex:
-
Layered System:
- A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary (like a load balancer, proxy, or API gateway) along the way.
- Intermediary servers can improve system scalability (load balancing, caching), enforce security policies, etc.
-
Code-On-Demand (Optional):
- Servers can temporarily extend or customize the functionality of a client by transferring executable code (e.g., JavaScript). This is the only optional constraint.
What is a RESTful API?
An API is considered "RESTful" if it adheres to the constraints of the REST architectural style. Most commonly, this means using:
- HTTP as the application protocol.
- URIs to identify resources.
- Standard HTTP methods (GET, POST, PUT, DELETE, PATCH) for actions on resources.
- JSON or XML as the format for resource representations.
- Stateless communication.
- HTTP status codes to indicate outcomes.
- Leveraging HTTP caching and headers.
Example RESTful Endpoints:
GET /users
: Retrieve a list of all users.GET /users/123
: Retrieve details for the user with ID 123.POST /users
: Create a new user (user data provided in the request body, usually JSON).PUT /users/123
: Replace the entire user resource with ID 123 (full user data in request body).PATCH /users/123
: Partially update the user resource with ID 123 (partial data in request body).DELETE /users/123
: Delete the user with ID 123.
In an Interview:
- Understand that REST is an architectural style, not a protocol.
- Be able to explain the key constraints, especially Client-Server, Stateless, Cacheable, and Uniform Interface (using HTTP methods and URIs for resources).
- Design simple, intuitive RESTful endpoints for common operations (Create, Read, Update, Delete - CRUD).
- Use standard HTTP methods and status codes appropriately.
- Mentioning HATEOAS shows a deeper understanding, even if you note it's not always fully implemented.