In REST (Representational State Transfer), a resource is an object or representation of something that has an identifier. Resources are accessed using URIs (Uniform Resource Identifiers). An example of a resource in REST would be a user in a system.
What is REST?
REST stands for Representational State Transfer. It is an architectural style for building web services and APIs that are lightweight, maintainable, and scalable. The REST architectural constraints are:
- Client-server – There should be a separation between the client and the server.
- Stateless – No client session state is stored on the server side.
- Cacheable – Responses should allow caching for performance.
- Uniform interface – Resources should be identifiable through URIs.
- Layered system – The client shouldn’t know if it’s connected directly to the server or going through intermediaries.
- Code on demand (optional) – Ability to send executable code from server to client when requested.
Some key principles of REST include:
- Everything is a resource – Objects/data are represented as resources.
- Resources are manipulated using standard HTTP methods like GET, POST, PUT, DELETE.
- Requests are stateless between client and server.
- Responses typically return representations of resources in JSON or XML.
What is a resource?
A resource is a key abstraction in REST. It represents an object or information that can be accessed or manipulated by the API.
Some examples of resources:
- A user
- A product
- A blog post
- A comment
- A video
Each resource has a unique identifier (UID). This allows the resource to be unambiguously addressed and manipulated in the system. The UID is called a Uniform Resource Identifier (URI).
For example, a specific user resource could have the URI:
/api/users/1234
Where “/api/users/” is the base URI for user resources and “1234” uniquely identifies that particular user.
Resources represent the “nouns” of the system – the objects. The HTTP methods represent the verbs – the actions that can be performed on the resources.
Resource representation
Resources are not passed directly between client and server. Instead a representation of the resource is sent. This could be in JSON, XML, HTML, or some other format that can communicate the data about that resource.
For example, the JSON representation of a user resource with ID 1234 might look like:
“`json
{
“id”: 1234,
“name”: “John Doe”,
“email”: “[email protected]”
}
“`
This JSON object communicates the details of user 1234 to the API consumer.
The server holds the actual resource internally in some database or other storage. But the client never directly accesses this internal representation. It only interacts with the external representation of the resource.
How are resources accessed?
REST uses HTTP protocol for accessing resources. The standard HTTP methods GET, POST, PUT, DELETE can be used.
HTTP Method | Action |
---|---|
GET | Retrieve a representation of the resource |
POST | Create a new resource |
PUT | Update the resource |
DELETE | Delete the resource |
For example:
- GET /api/users/1234 – Get user with ID 1234
- POST /api/users – Create a new user
- PUT /api/users/1234 – Update user 1234
- DELETE /api/users/1234 – Delete user 1234
The URI identifies the resource and the HTTP method defines what action to perform.
Conclusion
In summary, a resource in REST represents an object or information that can be manipulated by the API. Resources have unique IDs, different representations (JSON, XML), and are accessed using standard HTTP methods like GET, POST, PUT, DELETE. Common examples of resources include users, products, articles etc. Resources are central to the design of RESTful web services and APIs.