RDM LABSPORTUGUÊS

Technical article/ · 9 min read

How the Web Works: The Ecosystem Behind Every Request

A brief introduction to the layers that make the internet what it is today.

Laravel Saga · Season 0 · Episode 01

I want to start this episode with a statement that might sound exaggerated, but one I'll stand by until the very end: every web framework exists to solve a single problem. Whether it's Laravel, Rails, Django, or Next.js, they all take an HTTP message and return another HTTP message. Routes, controllers, databases, and templates are just ways of organizing the work that happens between one message and the next.

That's why, before opening a single Laravel file, we need to understand these messages properly. And I promise you: by the end of this episode, half of what seems like "magic" in a framework will start to feel obvious.

Client, Server, and... Text

HTTP (Hypertext Transfer Protocol) follows a request-response model. On one side, a client (a browser, a mobile app, curl, or another server) sends a request. On the other, a server returns a response. That's it.

And here's a detail that many people who have been working in web development for years have never stopped to notice: in HTTP/1.1, these messages are plain text. Take a look at this actual request:

httpPOST /posts HTTP/1.1
Host: myblog.com
Content-Type: application/x-www-form-urlencoded
Accept: text/html
Cookie: laravel_session=eyJpdiI6...
Content-Length: 38

title=My+first+post&body=Hello+world

And here's the response:

httpHTTP/1.1 302 Found
Location: /posts/42
Set-Cookie: laravel_session=eyJpdiI6...; path=/; httponly; samesite=lax
Content-Type: text/html; charset=UTF-8

Notice that every message has the same three parts:

  • Start line. In a request, it contains the method, path, and version (POST /posts HTTP/1.1). In a response, it contains the version, status code, and a reason phrase (HTTP/1.1 302 Found).
  • Headers. Name-value pairs that carry information about the message.
  • Body. Optional content, separated from the headers by a blank line.

You don't have to take my word for it. Open your terminal and run:

bashcurl -v https://example.com

Lines beginning with > show the request curl sent. Lines beginning with < show the response it received. You're looking at HTTP in its raw form, exactly as it travels across the connection.

Methods: What You Want to Do

The method expresses the intent of a request. It's the client telling the server, "I want to read this" or "I want to create that."

MethodIntentSafe?Idempotent?
GETRetrieve a resourceYesYes
HEADSame as GET, but without the response bodyYesYes
POSTCreate something or perform an actionNoNo
PUTReplace an entire resourceNoYes
PATCHModify part of a resourceNoNot necessarily
DELETERemove a resourceNoYes
OPTIONSAsk what is allowed (used in CORS)YesYes

Two words in this table deserve a minute of your attention.

Safe means the request shouldn't change anything on the server. Idempotent means that sending the same request once or ten times produces the same intended effect. Deleting post 42 twice still leaves post 42 deleted. Creating a post twice creates two posts.

This might sound academic, but it has very practical consequences. Browsers, proxies, and CDNs make decisions based on these rules: a GET request can be cached and repeated automatically; a POST request generally can't be treated the same way.

So, if you take away just one rule from this episode, make it this one: never modify data through a GET route. A search engine crawler that finds a link like /posts/42/delete will delete your post without any malicious intent whatsoever.

One detail that will come up again throughout this saga: HTML forms can only submit GET and POST requests directly. To simulate PUT, PATCH, or DELETE, frameworks use a hidden form field (in Laravel, it's called _method). This technique has a name: method spoofing.

Status Codes: What Happened?

If the method is the question, the status code is the summary of the answer. It's one of the first things the client checks, and status codes are organized into families:

FamilyMeaningCommon examples
1xxInformational101 Switching Protocols (WebSockets)
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirection301 Moved Permanently, 302 Found, 304 Not Modified
4xxClient error400, 401, 403, 404, 405, 422, 429
5xxServer error500, 502 Bad Gateway, 503, 504 Gateway Timeout

A few pairs confuse a lot of people, so let me break them down:

401 Unauthorized means, roughly, "I don't know who you are." Authentication is missing or invalid. 403 Forbidden means, "I know who you are, but you're not allowed to do this." Authorization is the issue.

404 Not Found means, "This doesn't exist." 405 Method Not Allowed means, "This exists, but it doesn't support this HTTP method."

422 Unprocessable Content means, "I understood your request, but the data is invalid." Remember this status code: it's what Laravel returns when validation fails in an API request, assuming the request is handled as a JSON/API request.

502 and 504 usually mean that the web server or an intermediary couldn't successfully communicate with the application or an upstream service. We'll look at exactly why this happens in the next episode.

And there's one status code that isn't part of the official HTTP specifications, but that you'll encounter quite often in Laravel: 419, used when the CSRF protection token has expired or is no longer valid. We'll get to CSRF in a moment.

Headers: The Context of the Conversation

Headers carry everything that isn't the content itself. Here are the ones you'll encounter most often:

HeaderWhereWhat it's for
HostRequestWhich website the client wants to reach (one server can host several)
Content-TypeBothThe format of the message body (application/json, text/html, multipart/form-data)
AcceptRequestThe format the client wants to receive
AuthorizationRequestCredentials (Bearer <token>)
Cookie / Set-CookieRequest / ResponseSending and setting cookies
LocationResponseWhere to redirect the client (usually with a 3xx response)
Cache-ControlBothCaching rules
OriginRequestWhere the request originated (relevant to CORS and CSRF)
Sec-Fetch-SiteRequestWhether the request originated from the same site or another site
X-Forwarded-For / X-Forwarded-ProtoRequestThe original client IP and protocol when a proxy is involved

I want to draw your attention to Accept. Laravel uses content negotiation, including this header and the request context, to determine how to respond to a validation error: with a redirect (for a browser-based form) or with a JSON response and a 422 status code (for an API). Throughout this saga, you'll see this behavior encapsulated in a method called $request->expectsJson().

HTTP Has No Memory

We've reached the protocol's most important characteristic, and it explains a lot of things: HTTP is stateless, meaning it doesn't maintain state between requests. Every request is independent. The server doesn't "remember" that you logged in during the previous request.

So how does login work? The answer is cookies. Follow along:

  1. You submit your email and password.
  2. The server verifies your credentials, creates a session (a record stored in a file, a database, or Redis, identified by a random ID), and responds with Set-Cookie: laravel_session=<id>.
  3. The browser stores that cookie and automatically sends it with subsequent requests to that domain.
  4. With each request, the server reads the ID, retrieves the session, and figures out who you are.
How the server remembers you1. Browser to Laravel: POST /login (email, pass). 2. Laravel to Sessions: creates session abc123. 3. Laravel replies to Browser: Set-Cookie: laravel_session. 4. Browser to Laravel: GET /dashboard + cookie. 5. Laravel to Sessions: looks up abc123. 6. Sessions replies to Laravel: user 7. 7. Laravel replies to Browser: 200 OK.Browserkeeps the cookieLaravelreads the sessionSessionsfile, database or RedisPOST /login (email, pass)creates session abc123Set-Cookie: laravel_sessionGET /dashboard + cookielooks up abc123user 7200 OK
How the server remembers you

In APIs consumed by mobile apps, a different approach is common: instead of a cookie, the client sends a token in the Authorization: Bearer <token> header with every request. The idea is the same: give the server a way to recognize you.

The Dangerous Side of Automatic Cookies

That "the browser sends it automatically" behavior comes at a cost. Imagine you're logged in to your bank's website and, in another tab, you visit a malicious website. That site might contain a hidden form that submits a POST request to your bank. The browser, doing exactly what it's designed to do, sends your session cookie along with it. And the bank assumes the request came from you.

This attack is called CSRF (Cross-Site Request Forgery), and three classic defenses are:

  1. A secret token generated by the server and included in every form. The malicious website can't read this token.
  2. The SameSite cookie attribute, which restricts when the browser sends cookies with requests originating from other sites.
  3. Checking the Origin and Sec-Fetch-Site headers, which the browser populates and an attacker generally can't forge from a cross-origin web page.
The attack and the three defencesMalicious site to Browser: hidden form. Browser to Your bank: POST + session cookie. Scenario No protection: The bank thinks it was you. The browser sends the session cookie along with the POST, and the bank has no way to tell the forged request from one of yours. Path: Malicious site, Browser, Your bank. Scenario Token: CSRF token. The forged form doesn't have the secret token, because the malicious site can't read it. The server rejects the request. Path: Malicious site, Browser. Browser to Your bank failing. Hint: in Laravel, the 419. Scenario SameSite: SameSite cookie. The request started on another site, so the browser doesn't even send the session cookie. As far as the bank knows, nobody is logged in. Path: Malicious site, Browser. Browser to Your bank failing. Scenario Origin: Origin and Sec-Fetch-Site. The browser marks the request as coming from another site, and the attacker can't forge those headers. The server refuses. Path: Malicious site, Browser. Browser to Your bank failing. Hint: Sec-Fetch-Site: cross-site.Malicious siteanother tabBrowseryou, logged inYour banktrusts the cookiehidden formPOST + session cookieMalicious siteanother tabBrowseryou, logged inYour banktrusts the cookiehidden formPOST + session cookieMalicious siteanother tabBrowseryou, logged inhidden formPOST + session cookieMalicious siteanother tabBrowseryou, logged inhidden formPOST + session cookieMalicious siteanother tabBrowseryou, logged inhidden formPOST + session cookie
The attack and the three defencesMalicious site to Browser: hidden form. Browser to Your bank: POST + session cookie. Scenario No protection: The bank thinks it was you. The browser sends the session cookie along with the POST, and the bank has no way to tell the forged request from one of yours. Path: Malicious site, Browser, Your bank. Scenario Token: CSRF token. The forged form doesn't have the secret token, because the malicious site can't read it. The server rejects the request. Path: Malicious site, Browser. Browser to Your bank failing. Hint: in Laravel, the 419. Scenario SameSite: SameSite cookie. The request started on another site, so the browser doesn't even send the session cookie. As far as the bank knows, nobody is logged in. Path: Malicious site, Browser. Browser to Your bank failing. Scenario Origin: Origin and Sec-Fetch-Site. The browser marks the request as coming from another site, and the attacker can't forge those headers. The server refuses. Path: Malicious site, Browser. Browser to Your bank failing. Hint: Sec-Fetch-Site: cross-site.Malicious siteanother tabBrowseryou, logged inYour banktrusts the cookieformwith cookieMalicious siteanother tabBrowseryou, logged inYour banktrusts the cookieformwith cookieMalicious siteanother tabBrowseryou, logged informwith cookieMalicious siteanother tabBrowseryou, logged informwith cookieMalicious siteanother tabBrowseryou, logged informwith cookie

No protection

The bank thinks it was you

The browser sends the session cookie along with the POST, and the bank has no way to tell the forged request from one of yours.

Token

CSRF token

The forged form doesn't have the secret token, because the malicious site can't read it. The server rejects the request.

in Laravel, the 419

SameSite

SameSite cookie

The request started on another site, so the browser doesn't even send the session cookie. As far as the bank knows, nobody is logged in.

Origin

Origin and Sec-Fetch-Site

The browser marks the request as coming from another site, and the attacker can't forge those headers. The server refuses.

Sec-Fetch-Site: cross-site

The attack and the three defences

Keep these three defenses in mind. Throughout the saga, you'll see Laravel using these mechanisms: SameSite is already configured on session cookies, and Laravel 13 combines token validation and origin checking in a single middleware.

HTTPS, HTTP/2, and HTTP/3 — A Quick Overview

HTTPS is the same HTTP protocol, but inside a tunnel encrypted with TLS. The messages are conceptually the same; the difference is that outsiders along the way can't read or tamper with them. Today, HTTPS is no longer optional in practice: several browser features, including secure cookies and certain security-related behaviors, depend on it.

HTTP/2 preserves the same semantics (methods, status codes, and headers), but replaces the plain-text message format used by HTTP/1.1 with binary frames and allows multiple requests to be handled concurrently over a single connection. From your application's perspective, the HTTP semantics remain the same.

HTTP/3 replaces TCP with QUIC, which runs over UDP and helps reduce latency, particularly when network conditions are unstable. Once again, the semantics remain the same.

And that's the lesson I want to leave you with: HTTP semantics have remained remarkably consistent since the 1990s. Requests, responses, methods, status codes, and headers are the same core concepts across versions. Learn them once, and they'll serve you for years to come.

Hands-On

Before moving on, I suggest playing around with these commands:

bash# Show only the response headers
curl -I https://laravel.com

# Send JSON and ask for JSON in return
curl -X POST https://httpbin.org/post \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{"title": "Test"}'

# Follow redirects and show everything
curl -vL http://github.com

Then open the Network tab in your browser's developer tools, reload any page, and click on one of the requests. You'll find exactly the three parts we've studied: the start line, headers, and body. Once you start seeing these things, you can't unsee them.

What to Take Away from This Episode

  • At its core, a web framework is a machine that transforms requests into responses.
  • The method expresses intent, the status code expresses the outcome, and the headers provide context.
  • HTTP is stateless; sessions and cookies create the illusion of continuity.
  • Automatically sent cookies require protection against CSRF.

Next Episode

The request has left the browser and traveled across the internet. But who's waiting for it on the other side? If you thought "PHP," I understand, but it's not quite that simple. Before PHP, there's an intermediary, and understanding that intermediary will solve a good number of deployment problems you'll encounter throughout your career.

In the next episode, we'll meet NGINX and follow the request's journey until it reaches your code. See you in Episode 02.

References and Recommended Reading

Official Documentation and Specifications

Further Reading

Book

  • HTTP: The Definitive Guide, by David Gourley and Brian Totty (O'Reilly). It's an older book, but it remains one of the most comprehensive explanations of the protocol.