REST — Explained with Examples
REST (Representational State Transfer) is an architectural style for designing networked applications using stateless HTTP operations on resources.
REST stands for Representational State Transfer, introduced by Roy Fielding in his 2000 doctoral dissertation. It defines a set of constraints for building web services that are scalable, stateless, and cacheable.
How REST Works
REST treats server data as resources, each identified by a URL. Clients interact with these resources using standard HTTP methods. A resource might be a user, a product, or an order — anything your application manages.
| HTTP Method | Operation | Example |
|---|---|---|
| GET | Read a resource | GET /api/users/1 |
| POST | Create a resource | POST /api/users |
| PUT | Replace a resource | PUT /api/users/1 |
| PATCH | Partial update | PATCH /api/users/1 |
| DELETE | Remove a resource | DELETE /api/users/1 |
REST APIs return representations of resources — typically JSON or XML — along with HTTP status codes that indicate success or failure.
Real-World Analogy
Think of a restaurant menu. Each menu item (resource) has a name and price (representation). You place an order (request) and the kitchen prepares it (server processes). The waiter brings your food (response). You don’t need to know how the kitchen works — that’s the separation of client and server. Each order is independent — that’s statelessness.
Example: REST API with Node.js
const express = require('express');
const app = express();
app.use(express.json());
let users = [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' }
];
// GET /api/users — list all users
app.get('/api/users', (req, res) => {
res.json(users);
});
// GET /api/users/1 — get single user
app.get('/api/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
user ? res.json(user) : res.status(404).json({ error: 'Not found' });
});
// POST /api/users — create user
app.post('/api/users', (req, res) => {
const user = { id: users.length + 1, ...req.body };
users.push(user);
res.status(201).json(user);
});
// DELETE /api/users/1 — delete user
app.delete('/api/users/:id', (req, res) => {
users = users.filter(u => u.id !== parseInt(req.params.id));
res.status(204).send();
});
app.listen(3000);Expected output: GET /api/users returns [{"id":1,"name":"Alice","email":"alice@example.com"},{"id":2,"name":"Bob","email":"bob@example.com"}]
REST Constraints
Six constraints define a truly RESTful system:
- Stateless — each request contains all information needed
- Client-Server — separation of concerns
- Cacheable — responses declare cacheability
- Uniform Interface — consistent resource identification
- Layered System — intermediaries (proxies, gateways) are transparent
- Code on Demand (optional) — server can send executable code
Related Terms
CRUD, GraphQL, HTTP, JSON, API Gateway
Related Tutorial
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro