REST (Representational State of Resource) is an architectural style for designing networked applications. It’s based on the idea that resources are identified by URIs, and can be manipulated using a fixed set of operations.
What is REST?
At its core, REST is about interacting with resources over the web. A resource can be anything: a user, a product, an image, etc. Each resource is identified by a unique identifier called a URI (Uniform Resource Identifier).
In a RESTful system, clients and servers communicate using HTTP requests and responses. The client sends a request to the server, specifying the action it wants to perform on a particular resource. The server then processes the request and returns a response.
Key Principles of REST
There are several key principles that make an application RESTful:
- Resource-based: Everything in REST is a resource.
- Client-Server Architecture: The client and server are separate, with the client making requests to the server to access or modify resources.
- Stateless: Each request from the client contains all the information necessary for the server to process it.
- Cacheable: Responses from the server can be cached by the client to reduce the number of requests made.
- Uniform Interface: A uniform interface is used to communicate between client and server, including HTTP methods (GET, POST, PUT, DELETE), URI syntax, and standard HTTP status codes.
HTTP Methods
In RESTful programming, HTTP methods are used to manipulate resources:
- GET: Retrieve a resource
- POST: Create a new resource
- PUT: Update an existing resource
- DELETE: Delete a resource
These methods provide a standardized way of interacting with resources over the web.
Hypermedia Controls
Hypermedia controls, also known as HATEOAS (Hypermedia As The Engine Of Application State), are used to define the relationships between resources. They allow clients to discover and navigate to related resources without prior knowledge of their existence or location.
In JSON-based APIs, hypermedia controls can be represented using links, which contain information about the relationship type, the URL of the related resource, and the HTTP method to use when accessing it.
Example
Let’s consider a simple example of a RESTful API for managing users. The API uses a custom media type application/json+userdb
to represent resources.
Request
GET /
Accept: application/json+userdb
Response
{
"version": "1.0",
"links": [
{
"href": "/user",
"rel": "list",
"method": "GET"
},
{
"href": "/user",
"rel": "create",
"method": "POST"
}
]
}
In this example, the client requests the base resource /
and receives a response with links to related resources. The client can then use these links to navigate to other resources.
Request
GET /user
Accept: application/json+userdb
Response
{
"users": [
{
"id": 1,
"name": "Emil",
"country": "Sweden",
"links": [
{
"href": "/user/1",
"rel": "self",
"method": "GET"
},
{
"href": "/user/1",
"rel": "edit",
"method": "PUT"
},
{
"href": "/user/1",
"rel": "delete",
"method": "DELETE"
}
]
}
],
"links": [
{
"href": "/user",
"rel": "create",
"method": "POST"
}
]
}
The client can then use the links to retrieve, update or delete individual users.
Conclusion
RESTful programming is an architectural style that provides a standardized way of interacting with resources over the web. By using HTTP methods, hypermedia controls and a uniform interface, developers can create scalable, maintainable and flexible APIs that are easy to understand and use.
In this tutorial, we’ve covered the basics of RESTful programming, including key principles, HTTP methods and hypermedia controls. We’ve also seen an example of how these concepts can be applied in practice.