DEV Community

Cover image for Day 24 of #100DaysOfCode — Handling Request (Query, Params, Body)
M Saad Ahmad
M Saad Ahmad

Posted on

Day 24 of #100DaysOfCode — Handling Request (Query, Params, Body)

When a client communicates with a server, it sends data in different ways depending on what it wants to achieve. Today (Day 24), I learned how to handle three important parts of every HTTP request: query parameters, route parameters, and the request body.

These are all ways to send data to an API, but each one has its own purpose. Understanding them clearly makes backend development so much cleaner.


🔍 What "Request Data" Means in an API

Clients usually send data to a backend in three common ways:

  • Query Parameters
  • Route (Path) Parameters
  • Request Body

Let’s break them down with simple explanations and practical examples.


1️⃣ Query Parameters — For Searching, Filtering, Sorting, Pagination

Query parameters are added after the ? in a URL.

They are best used for:

  • Searching
  • Filtering results
  • Sorting
  • Pagination
  • Anything optional or user-controlled in a URL

Example:

GET /products?category=phone&sort=price
Enter fullscreen mode Exit fullscreen mode

Here:

  • category=phone
  • sort=price

These are query params.

💡 Real-world usage

If you have a search bar on the frontend, you might call your backend like this:

http://localhost:5000/search?q=${searchTerm}
Enter fullscreen mode Exit fullscreen mode

Notice the q= part? That’s the key by which your backend will read the value.

It could be anything like:

  • q=apple
  • q=samsung
  • q=laptop

Your backend receives that value and performs the search accordingly.


2️⃣ Route Params — For Identifying a Specific Item

Route (or path) parameters are part of the URL structure itself, not added after ?.

You use them when you need a specific resource.

Example:

GET /users/123
Enter fullscreen mode Exit fullscreen mode

Here, 123 is a route param — usually an ID.

Route params are perfect when you want:

  • A specific user
  • A specific product
  • A specific order

In Express.js:

app.get('/users/:id', (req, res) => {
  console.log(req.params.id); // 123
});
Enter fullscreen mode Exit fullscreen mode

3️⃣ Request Body — For Sending Data (Forms, JSON, etc.)

The request body is used when you need to send structured data, like:

  • Login credentials
  • Registration details
  • Form submissions
  • Creating new data
  • Updating existing data

This is used mainly with:

  • POST
  • PUT
  • PATCH

Example:

POST /login
Body: { "email": "test@example.com", "password": "secret" }
Enter fullscreen mode Exit fullscreen mode

💡 Real-world usage

Imagine a registration form. When the user clicks “Submit,” your frontend would send the form data like:

fetch("http://localhost:5000/register", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name, email, password }),
});
Enter fullscreen mode Exit fullscreen mode

On the backend (Express.js):

app.post('/register', (req, res) => {
  console.log(req.body); 
});
Enter fullscreen mode Exit fullscreen mode

And yes — during local development you’ll see URLs like:

http://localhost:5000/register
Enter fullscreen mode Exit fullscreen mode

Here:

  • 5000 = backend server port
  • /register = endpoint

In production, this becomes:

https://api.yourdomain.com/register
Enter fullscreen mode Exit fullscreen mode

The port is usually hidden because servers handle that internally.


🧠 Quick Summary Table

Data Type Where It Appears Use Case Example
Query Params After ? in the URL Search, filter, sort, pagination /search?q=phone
Route Params Inside the URL path Fetch a specific resource /users/123
Request Body Inside the HTTP request Submit forms, create/update data { email, password }

🎯 Final Thoughts

Learning how data flows through an API feels like a milestone. I finally understand why we have queries, route params, and bodies — not just how to use them.

If you're also learning backend development, mastering this concept early makes everything else that comes later (authentication, CRUD operations, search features, etc.) so much easier.

Happy coding!

Top comments (0)