Deep Dive into REST: Key Concepts, Terms, Best Practices with Examples.
REST Comprehensive Exploration from a Developer’s Point of View.
Greetings, everyone! With over two and a half years of experience crafting APIs, whether professionally or in pursuit of knowledge, I’ve been immersed in the realm of REST daily. Initially, I held the misconception that REST solely involved returning JSON responses from the server. Recognizing this as a noteworthy point for discussion, I embark on this exploration, aiming to glean insights from various resources to broaden our understanding of REST.

What is REST?
REST, or Representational State Transfer, is an architectural style for designing networked applications. It was introduced by Roy Fielding in his doctoral dissertation in 2000. REST is not a protocol but a set of principles that guide the design of web services and APIs (Application Programming Interfaces).
Key concepts associated with REST
- Resources: In REST, everything is considered a resource, which can be a physical entity, a concept, or a service. Each resource is identified by a URI (Uniform Resource Identifier).
- HTTP Methods (Verbs): RESTful services use standard HTTP methods to perform operations on resources. The most common HTTP methods used in RESTful services are:
GET: Retrieve a representation of the resource.
POST: Create a new instance.
PUT: Update an existing resource or create a new one if it doesn’t exist.
DELETE: Remove a resource.
PATCH: Partially update a resource.
OPTIONS: Retrieve information about the communication options for the target resource.
3. Representations: Resources can have different representations, such as JSON or XML.
Clients interact with these representations, and the server is responsible for converting between the representation and the internal state of the resource.
4. Statelessness: Each request from a client to a server must contain all the information needed to understand and process the request. The server should not store any information about the client’s state between requests.
5. Unique Interface: The uniform interface constraint is a central principle of REST. It simplifies and decouples the architecture, making it more scalable and flexible. The uniform interface is characterized by:
- Identification of Resources: Each resource is identified by URI.
- Manipulation of Resource through representations: Clients manipulate resources through the representation provided by the server.
- Self-descriptive Messages: Messages include information about how to process them.
Deeper Dive
Let’s look into above mentions concepts in deep.
Representations and Content Negotiation
In REST, resources can have different representations, such as JSON, XML, or even HTML. This allows clients to request data in a format they understand and can handle. Content negotiation, facilitated by the Accept
and Content-Type
headers in HTTP ensure that both clients and servers agree on the format of the representation.
Resources and URIs
Resources represent entities in the system that can be identified and manipulated. For example, in a blogging application, articles, comments, and authors could be considered resources.
A URI (Uniform Resource Identifier) is a string of characters that uniquely identifies a particular resource. URIs can be further classified into two types: URLs and URNs.
URN (Uniform Resource Name): A URN is a type of URI that identifies a resource by name in a particular namespace but doesn’t provide information on how to locate the resource. Eg ->
urn:isbn:0451450523
Components of a URN:
- Namespace Identifier: Specifies the namespace to which the URN belongs.
- NID (Namespace-specific Identifier): Specifies the unique identifier assigned to the resource within the namespace.
URL (Uniform Resource Locator): A URL is a specific type of URI that includes information on how to locate a resource. It provides the means to access the resource through a specific protocol and location details.
Components of a URL:
- Scheme: Specifies the protocol or scheme used to access the resource. Like
http
,https
,ftp
. - Host: Specifies the domain name or IP address of the server hosting the resource. Eg → 8.8.8.8 or www.google.com
- Path: Specifies the specific resource or location on the server. Like
/search
for Google Search Engine. - Query Parameters: Additional parameters that can be included for data retrieval. Eg →
?q=ip+of+google.com
- Fragment: Specifies a specific section or anchor within the resource or page. Eg →
#section1
. - Example:
https://www.example.com/resource?category=books#section2
Relationship b/w URL and URI
- All URLs are URIs
- Not all URIs are URLs
Manipulation of Resources Through Representations
In REST, resources have different representations, such as JSON, XML, or HTML. Clients interact with these representations to perform operations on resources. The server is responsible for converting between the internal state of the resource and its external representation.
Examples
Consider we are working on a blog application
- GET Request to Retrieve Article Representation
GET /articles/123
The server responds with a representation of the article in JSON.
{
"id": 123,
"title": "RESTful Principles",
"content": "An in-depth explanation of RESTful principles.",
"author": "John Doe"
}
- PUT Request to Update Article
PUT /articles/123
Content-Type: application/json
{
"id": 123,
"title": "RESTful Principles Explained",
"content": "A comprehensive guide to understanding RESTful principles.",
"author": "John Doe"
// Updated article details
}
Self-descriptive Messages
RESTful services use self-descriptive messages to convey information about the processing of requests and responses. This includes the use of HTTP headers, status codes, and other metadata that accompany the actual data being transferred.
Example
- GET Request to Retrieve List of Articles
GET /articles
HTTP/1.1 200 OK
Content-Type: application/json
{
"articles": [
{
"id": 123,
"title": "RESTful Principles Explained",
"author": "John Doe"
// Other article details
},
// Additional articles
],
"links": [
{
"rel": "next",
"href": "/articles?page=2"
},
// Other links
]
}
In this response, the server not only provides the requested data (list of articles) in JSON format but also includes metadata like the Content-Type and links to related resources (pagination links in this case).
Best Practices for Designing RESTful APIs
First of all, I want to make clear that these are not my standards but either I follow these or gather from resources.
Use Nouns for Resource URIs
Good Practice: Choose resource URIs that represent nouns rather than verbs. For Example
/users
for a collection of users./articles/{article_id}
for a specific article.
Why: Nouns make the URIs more intuitive and align with the idea of resources.
Versioning
Good Practice: Include version information in your API to manage changes over time. For example:
/v1/users
for the first version of the user's resource.
Why: Versioning helps maintain backward compatibility when introducing changes to the API.
Use Plural Nouns for Collections
Good Practice: Use plural nouns for resources URIs representing collections. For Example:
/users
for a collection of users./books
for a collection of books.
Why: Plural Nouns make it clear that URI represents collections of resources.
Use HTTP Status Codes Appropriately
Good Practice: Use HTTP status codes to indicate the success or failure of a request. For Example:
200 OK
for a successful GET request.201 Created
for a successful POST request.404 Not Found
for a resource that doesn't exist.
Why: Proper use of status code enhances the clarity of API’s responses.
To learn more about status codes you can check this 👇
HATEOAS (Hypermedia as the Engine of Application State)
Good Practice: Include hypermedia links in your responses to guide clients through the application state.
Consider a basic API for managing a collection of books. When a client requests information about a specific book, the server responds with the book details and includes hypermedia links that suggest possible actions a client can take.
GET /books/123
{
"id": 123,
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams",
"published_year": 1979,
"links": [
{
"rel": "self",
"href": "/books/123"
},
{
"rel": "edit",
"href": "/books/123/edit"
},
{
"rel": "delete",
"href": "/books/123/delete"
}
]
}
Why: With HATEOAS, the client doesn’t need to be aware of specific URIs for editing or deleting a book; it can discover these actions dynamically by following the hypermedia links provided in the response.
Django DRF provides awesome support for browsable APIs.
Pagination for Large Collections
Good Practice: Implement pagination for resource collections to manage large datasets. For example:
/articles?page=1&limit=10
for the first page of articles with a limit of 10 per page.
Why: Pagination improves performance, and decreases load and response times for clients.
Use Proper Authentication
Good Practice: Implement secure authentication mechanisms (e.g., OAuth, API keys) to control access to your API.
Why: Authentication ensures that only authorized users or systems can interact with your resources.
Provide Consistent and Predictable URIs
Good Practice: Keep URIs consistent and predictable. For example:
/articles/{article_id}/comments
for comments related to a specific article.
Why: Consistency makes it easier for developers to understand and use the API.
Real-World Examples of RESTful Concepts
Resources and URIs
Example: Blogging Application
- Resource: Article
- URI:
/articles/{article_id}
HTTP Methods in Action
Example: Social Media Platform
- Resource: User Profile
- HTTP Methods
GET: Retrieve user profile information (GET /users/{user_id}).
POST: Create a new user profile (POST /users).
PUT: Update an existing user profile (PUT /users/{user_id}).
DELETE: Remove a user profile (DELETE /users/{user_id}).
Representations and Content Negotiation
Example: E-commerce API
- Resource: Product
- Representations: JSON, XML
- Content Negotiation:
1. Client specifies the desired representation in the Accept header (Accept: application/json).
2. Server responds with the requested representation.
Stateless Communication
Example: Task Management System
- Each request to retrieve, create, update, or delete a task is independent.
- The server doesn’t store information about the client’s state between requests.
- Clients include all necessary information in each request for the server to understand and process it.
Conclusion
In summary, understanding REST goes beyond perceiving it as a mechanism for returning JSON responses. It encompasses core principles of resource representation, HTTP methods, and statelessness. Best practices like using nouns for URIs, versioning, and HATEOAS contribute to effective API design.
Real-world examples demonstrate the versatility of REST in applications. This exploration serves as a guide for developers, emphasizing adherence to REST principles and thoughtful design for building robust networked applications.
If you are a REST expert and reading my article and have any corrections or improvements please comment under the post. I will update the article accordingly after some more research.
Follow Rahul Beniwal for more related content.
Other click-worthy articles by me