Day 27 of #100DaysOfCode — REST API
Source: Dev.to
What Is a REST API?
Think of a REST API like a waiter in a restaurant:
- You (the client/app) request food.
- The kitchen (server/database) prepares it.
- The waiter (REST API) takes your request, delivers it, and brings the result back.
You never go into the kitchen; you communicate only through the waiter — using standard, agreed‑upon phrases.
So What Is REST?
REST (Representational State Transfer) is a set of rules that allow two applications to communicate over the internet. The client uses standard HTTP methods to talk to a server and fetch or change data.
Core HTTP Methods in REST APIs
| Method | Purpose |
|---|---|
| GET | Retrieve data |
| POST | Create new data |
| PUT | Replace an entire existing resource |
| PATCH | Update part of an existing resource |
| DELETE | Delete a resource |
Real-World Example of a REST API
When your weather app loads, it might send a request like this:
GET https://api.weather.com/city=karachi
The server responds with JSON data:
{
"city": "Karachi",
"temperature": "31°C",
"condition": "Sunny"
}
Your app displays the weather — thanks to the API.
What Is a “Resource” in REST?
A resource is any piece of data your API deals with.
Examples
- users
- posts
- products
- orders
Each resource has a unique URL (called an endpoint):
/users
/posts
/products
RESTful Route Naming Rules
REST focuses on nouns, not verbs.
Correct RESTful Routes
Get all users
GET /users
Create a user
POST /users
Get a single user
GET /users/:id
Update a user
PUT /users/:id
PATCH /users/:id
Delete a user
DELETE /users/:id
Wrong Route Naming (don’t do this)
POST /createUser
GET /getAllUsers
DELETE /deleteUser
These use verbs in the URL, which breaks REST conventions.
Query Parameters in REST APIs
Query parameters allow filtering, searching, or customizing results.
GET /users?role=admin
GET /products?limit=10&page=2
GET /posts?sort=latest
Request & Response Structure
Request
- params – values inside the URL (e.g.,
/users/:id) - query – filtering/pagination (e.g.,
?page=2) - body – data for POST/PUT/PATCH requests
- headers – metadata (auth tokens, content‑type, etc.)
Response
- status code
- JSON body
- headers
- optional metadata (pagination info, timestamps, etc.)
Common REST API Status Codes
Success
- 200 OK – Request successful
- 201 Created – New resource created
- 204 No Content – Successful but no response body
Client Errors
- 400 Bad Request – Invalid input
- 401 Unauthorized – Authentication needed
- 403 Forbidden – Authenticated but not allowed
- 404 Not Found – Resource doesn’t exist
Server Errors
- 500 Internal Server Error – Something broke on the server
Idempotency in REST
“Idempotent” means sending the same request multiple times yields the same result.
| Method | Idempotent? | Why |
|---|---|---|
| GET | ✔️ Yes | Fetching data doesn’t change anything |
| PUT | ✔️ Yes | Replaces the resource with the same data each time |
| DELETE | ✔️ Yes | Deleting again leaves the state unchanged |
| PATCH | ⚠️ Sometimes | Depends on how the backend handles partial updates |
| POST | ❌ No | Creates new resources → duplicates |
Final Summary
A REST API is a messenger between your app and a server. It lets you create, read, update, and delete data using standard HTTP methods. If you understand:
- resources
- routes
- verbs (GET, POST, PUT, DELETE)
- query parameters
- status codes
- idempotency
…then you already grasp the core of REST APIs.
Happy coding!