Welcome to the world of web development!
A REST API (Representational State Transfer Application Programming Interface) is a way for different software programs or systems to communicate with each other over the internet, much like how humans use languages to communicate. Imagine youβre at a restaurant: you (the client) give your order (a request) to the waiter (the API), and the waiter passes your order to the kitchen (the server). The kitchen prepares your food (processes the request), and the waiter brings it back to you (the response). Just like how a menu lists available food options, the API lists different commands or actions that can be requested from the server, and the server responds with the information or data you asked for. REST APIs are used everywhere β from checking weather forecasts to sending messages on social media.
π§βπ» REST - Representational State Transfer
REST is an architectural style used for designing networked applications.
π¦ REST API Characteristics
- π Stateless
- π Resource-based (e.g.,
/users, /products)
- π Uses standard HTTP methods
- π¬ Communicates via JSON/XML (usually JSON)
π REST API Standards
| Standard |
Description |
| Stateless |
No session info stored on the server. Each request is independent. |
| Uniform Interface |
Consistent structure: URIs, methods, and formats. |
| Cacheable |
Responses can be cached to improve performance. |
| Layered System |
Client doesnβt know if itβs talking to the real server or a proxy. |
| Code on Demand (Optional) |
Servers can send executable code to clients (e.g., JavaScript). |
π§Ύ HTTP Methods
| Method |
Use Case |
| GET |
π Retrieve data from the server. |
| POST |
β Create a new resource. |
| PUT |
π Update an existing resource (replace). |
| PATCH |
π©Ή Partially update a resource. |
| DELETE |
β Remove a resource. |
π€ HTTP Request Structure
GET /api/users HTTP/1.1
Host: example.com
Authorization: Bearer <token>
Content-Type: application/json
Components:
| Part |
Description |
| Request Line |
Method + URL + HTTP Version |
| Headers |
Key-Value pairs for metadata (Auth, Content-Type, etc.) |
| Body |
Payload for POST/PUT/PATCH requests |
π₯ HTTP Response Structure
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 1,
"name": "Alice"
}
Components:
| Part |
Description |
| Status Line |
HTTP version + Status code + Status message |
| Headers |
Metadata (Content-Type, Caching, etc.) |
| Body |
Actual response data (typically JSON) |
| Header |
Purpose |
Authorization |
For authentication tokens |
Content-Type |
Indicates format of request/response body |
Accept |
Clientβs preferred response format |
Cache-Control |
Caching directives |
User-Agent |
Info about the client making the request |
π‘ HTTP Response Codes
| Code |
Meaning |
Description |
200 OK |
β
Success |
Request was successful |
201 Created |
π Resource created successfully |
Β |
204 No Content |
β
Success with no body |
Β |
400 Bad Request |
β οΈ Client error (invalid input) |
Β |
401 Unauthorized |
π Missing or invalid auth |
Β |
403 Forbidden |
π« Authenticated but no permission |
Β |
404 Not Found |
β Resource not found |
Β |
500 Internal Server Error |
π₯ Something broke on the server |
Β |
π― Summary
- Clients make requests to Servers using HTTP.
- REST APIs follow consistent, stateless principles.
- HTTP methods define CRUD operations.
- Headers and status codes convey important metadata.
- Clients can be browsers, apps, devices; Servers can serve web, app, DB, files, and more.