DEV Community

Cover image for Day 27 of #100DaysOfCode — REST API
M Saad Ahmad
M Saad Ahmad

Posted on

Day 27 of #100DaysOfCode — REST API

Whether you realize it or not, you’ve already been using REST APIs every time an app sends a request and receives a response.
Your weather app, your social feeds, your login screen — they’re all quietly talking to servers through REST APIs in the background.

Today, on Day 27, I focused on truly understanding how the request–response cycle works behind the scenes.


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

Here are the most common ones:

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
Enter fullscreen mode Exit fullscreen mode

The server responds with JSON data:

{
  "city": "Karachi",
  "temperature": "31°C",
  "condition": "Sunny"
}
Enter fullscreen mode Exit fullscreen mode

Your app displays the weather — thanks to the API.


📁 What Is a “Resource” in REST?

A resource is basically any piece of data your API deals with.
Examples:

  • users
  • posts
  • products
  • orders

Each resource has a unique URL (called an endpoint).

For example:

/users
/posts
/products
Enter fullscreen mode Exit fullscreen mode

🛣 RESTful Route Naming Rules

REST focuses on nouns, not verbs.

✅ Correct RESTful routes:

Get all users

GET /users
Enter fullscreen mode Exit fullscreen mode

Create a user

POST /users
Enter fullscreen mode Exit fullscreen mode

Get a single user

GET /users/:id
Enter fullscreen mode Exit fullscreen mode

Update a user

PUT /users/:id
PATCH /users/:id
Enter fullscreen mode Exit fullscreen mode

Delete a user

DELETE /users/:id
Enter fullscreen mode Exit fullscreen mode

❌ Wrong route naming (don’t do this)

POST /createUser
GET /getAllUsers
DELETE /deleteUser
Enter fullscreen mode Exit fullscreen mode

These use verbs in the URL — which breaks REST conventions.


🔎 Query Parameters in REST APIs

Query params allow filtering, searching or customizing results.

Examples:

GET /users?role=admin
GET /products?limit=10&page=2
GET /posts?sort=latest
Enter fullscreen mode Exit fullscreen mode

📡 Request & Response Structure

A Request contains:

  • 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.)

A Response contains:

  • 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 — You’re 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 gives the same result.

Here’s how each HTTP method behaves:

Method Idempotent? Why
GET ✔️ Yes Fetching data doesn't change anything
PUT ✔️ Yes Replaces the resource with the same data every time
DELETE ✔️ Yes Deleting again still results in the same state
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 params
  • status codes
  • idempotency

…then you already grasp the core of REST APIs.

Happy coding!

Top comments (0)