HTTP Messages, Status Codes & Headers

Beginner · 40 min

Inside the request and response — methods, status code categories, common headers, and why HTTP has no memory between calls.

Before thisHow the Internet and the Web Work

Traversy Media · 40 min

HTTP Messages, Status Codes & Headers

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Traversy MediaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An HTTP request is a structured message with a method, a path, headers, and sometimes a body; a response mirrors that shape with a status code instead of a method.
  • The method states the intent of a request — GET to read, POST to create, PUT or PATCH to update, DELETE to remove — and servers are expected, not forced, to honor that intent.
  • Status codes are grouped by their first digit — 2xx success, 3xx redirect, 4xx client error, 5xx server error — so you can judge the category of a response before reading the specific code.
  • Headers carry metadata about the message itself, separate from its content, like Content-Type describing the body's format or Authorization carrying credentials.
  • HTTP is stateless by design — each request is handled with no memory of previous ones — which is exactly why mechanisms like cookies and tokens had to be invented to keep users logged in.

Opening the envelope

The last lesson established that browsers and servers exchange requests and responses, and that this exchange happens over HTTP. This lesson opens that envelope. What is actually inside an HTTP message, and what do all those codes and header names you’ve seen in a network tab actually mean?

It helps to think of HTTP as a strict letter format, not a conversation. Every request and every response follows the same predictable shape, which is precisely what lets browsers, servers, proxies, and caches — software written by completely different teams, decades apart — all understand each other without ever agreeing on anything beyond the spec.

Anatomy of a request

An HTTP request has four parts. First, a method, which states what you’re trying to do. Second, a path, which identifies what resource on the server you’re targeting — /users/42, /api/cart, whatever it is. Third, a set of headers, key-value pairs carrying metadata about the request. Fourth, optionally, a body — the actual data you’re sending, present on something like a form submission and absent on a simple page load.

The method is worth dwelling on, because new developers often treat it as decoration when it’s actually a contract. GET means “give me this resource” and should never change anything on the server — a GET request is safe to retry, cache, or prefetch, because nothing is supposed to happen as a side effect of making it. POST means “create something new,” typically with data in the body. PUT means “replace this resource entirely with what I’m sending.” PATCH means “update part of this resource.” DELETE means “remove this resource.” None of these are enforced by the protocol itself — a server could technically delete your account on a GET request — but violating this contract breaks caching, breaks browser back-button behavior, and breaks every tool built assuming the convention holds. Follow it.

Anatomy of a response

A response mirrors the request’s shape but swaps the method for a status code, plus usually a short reason phrase (“OK,” “Not Found”). It still has headers, and it still usually has a body — the actual HTML, JSON, or image data you asked for.

Status codes are organized by their leading digit into five categories, and this is genuinely one of the most useful things to have memorized as a developer, because it lets you triage a failure before reading a single word of documentation:

  • 1xx — Informational. Rare to encounter directly; the server is saying “continue” or acknowledging a protocol upgrade mid-exchange.
  • 2xx — Success. The request was received, understood, and acted on. 200 OK is the workhorse; 201 Created confirms a POST made something new; 204 No Content confirms success with nothing to send back.
  • 3xx — Redirection. The resource you want lives somewhere else now. 301 Moved Permanently tells the browser (and search engines) to update its records; 302 Found says “it’s over there, just for now.”
  • 4xx — Client error. You asked for something wrong. 404 Not Found is the famous one; 400 Bad Request means the request itself was malformed; 401 Unauthorized means you need to authenticate; 403 Forbidden means you did authenticate, and you’re still not allowed.
  • 5xx — Server error. The request was fine; the server choked trying to fulfill it. 500 Internal Server Error is the generic catch-all; 503 Service Unavailable usually means the server is overloaded or down for maintenance.

Notice the pattern: 4xx is “you did something the server won’t accept,” 5xx is “the server broke while handling something it otherwise understood.” When you’re debugging a failed request, that distinction tells you immediately where to look first — your request, or their server.

Headers: metadata about the message

Headers are easy to skim past, but they’re doing real work. A few come up constantly enough to know by name:

Content-Type tells the receiver what format the body is in — application/json, text/html, image/png — so it knows how to parse what follows instead of guessing.

Authorization carries credentials, typically a token, proving who’s making the request. This is how an API tells a logged-in request apart from an anonymous one.

Cache-Control tells browsers and intermediate caches how long a response is good for before it needs to be re-fetched, which is a large part of why some sites feel instant on a second visit and others re-download everything every time.

There are dozens more — User-Agent, Accept, Set-Cookie — but these three cover the majority of what you’ll touch as a frontend developer early on.

The stateless catch

Here’s the detail that trips people up the most: HTTP has no memory. Every single request is handled by the server as if it’s the very first one it’s ever received from you, with zero built-in awareness of anything you did a moment ago — even if you just logged in thirty seconds earlier on the same page.

This isn’t an oversight; it’s deliberate. Statelessness is what lets any server in a cluster handle any request, lets caches work predictably, and keeps the protocol simple enough to have survived forty years of growth. But it does mean that anything resembling “being logged in” is not a feature of HTTP at all — it’s something built on top, using mechanisms like cookies (small pieces of data the browser stores and automatically resends with every request to the same site) or tokens sent in an Authorization header. Every time you see “session,” “cookie,” or “JWT” in frontend work, you’re looking at an engineered workaround for the fact that HTTP, by design, forgets you the instant your response arrives.

With requests, responses, and this stateless foundation in hand, you’re ready for what the browser actually does with the response body once it arrives — which is the subject of the next lesson.

Check yourself

  1. 01What are the four main parts of an HTTP request?

    A method, a path (what resource it's targeting), a set of headers, and optionally a body carrying data.

  2. 02What's the difference between PUT and PATCH?

    PUT is expected to replace the entire resource with what you send; PATCH is expected to apply a partial update, changing only the fields included.

  3. 03If a response comes back with a status code in the 500s, whose fault is it most likely to be?

    The server's. 5xx codes mean the server failed to fulfill a request it otherwise understood, as opposed to 4xx, which means the client's request itself was the problem.

  4. 04Why does a logged-in website need cookies or tokens at all, given that you already sent your password once?

    Because HTTP is stateless — the server treats every request as if it's the first one it's ever seen from you. Without a cookie or token re-identifying you on each request, the server would have no way to know you'd already logged in.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark