What Is a Web Request and How Does a Server Respond?

What Is a Request? From Pizza to Code 🍕

When someone says, “The browser sends a request to the server,” it often sounds mysterious.
What kind of request? Where does it go? And who answers it?

Let’s break it down with a simple example:

  • You = the user
  • Browser = the waiter
  • Server = the chef

You tell the waiter:
👉 “Bring me a pizza!”
The waiter goes to the kitchen and says to the chef:
👉 “The customer wants a pizza.”
The chef makes it and gives it back.

That’s basically how the web works:

  • You open a website.
  • The browser sends an HTTP request to the server.
  • The server sends back a response: a page, data, an image, or an error.

Types of Requests — Super Simple

GET — “Give me this thing”

Example:
You open example.com/about.
The browser sends a request:
“Hey server, give me the page /about”

POST — “Here’s some info, do something with it”

Example:
You fill out a registration form.
The browser sends:
“Hey server, here’s a name, email, and password. Please register this user.”


What Can the Server Send Back?

  • An HTML page
  • JSON data
  • A file (like an image or a PDF)
  • An error (like 404 – page not found, or 500 – server error)
  • Or even nothing (just a silent “OK”) — when something happens behind the scenes and there’s no need to send anything back.

For example:
You click a “like” button. The server quietly saves that info in the database.

Request:
POST /like/12345

Server response:
HTTP/1.1 200 OK

That’s it — no data, no HTML, no JSON. Just a simple: “Got it.”


Requests Happen Inside Code Too

The idea of asking and answering (a request and a response) is everywhere — not just on the internet.


Python Example

def get_temperature(city):
    if city == "Stockholm":
        return 5
    else:
        return "Unknown city"

temp = get_temperature("Stockholm")  # <- request
print(temp)  # <- response: 5

The function is like a mini-server. You ask it something, and it replies.


Java Example

public class WeatherService {
    public static int getTemp(String city) {
        if (city.equals("Tokyo")) {
            return 20;
        }
        return -999; // error
    }

    public static void main(String[] args) {
        int t = getTemp("Tokyo");  // <- request
        System.out.println(t);     // <- response
    }
}

Same logic — we request something from a method, and it returns data.


PHP Example

function getUser($id) {
    if ($id === 1) {
        return "Alice";
    }
    return "Unknown";
}

echo getUser(1); // <- request

Why This Matters

When you build websites, apps, or even just learn coding, you’re constantly working with requests:

  • The browser sends requests to the server.
  • Your code might send a request to a database.
  • One function might request help from another function.

If you get this idea —
👉 “I’m asking for something. Something gives me an answer.”
— it becomes way easier to understand how code works.


Want to Play Around?

Try this in your browser or terminal:

curl https://httpbin.org/get

Or in Python:

import requests
r = requests.get("https://httpbin.org/get")
print(r.text)

Leave a Reply

Your email address will not be published. Required fields are marked *