What this page is for

Intent: know what an HTTP request and response look like, what the methods and status codes mean, and why HTTP forgets you between requests.
You’ll learn: the four parts of a message · safe and idempotent · GET vs POST · PUT vs PATCH · status code families · 401 vs 403 · stateless HTTP and cookies.
Time: about 7 minutes. Every section has a longer version on the detailed HTTP message page.

Words you need first

  • HTTP (HyperText Transfer Protocol): the rule set browsers and servers use to talk. The browser asks, the server answers.
  • Request: a message the client (browser, app, curl) sends to ask for something.
  • Response: the server’s message back, with a result and optionally an answer.
  • Method: the verb of a request, such as GET (read) or POST (create or act).
  • Status code: a three-digit number in the response that says how it went, e.g. 200 means OK.
  • Header: a Name: value line of extra facts, e.g. Content-Type: application/json.
  • Body: the payload after the headers, e.g. the JSON answer.
  • Safe: the request changes nothing on the server. Read only.
  • Idempotent: sending the same request twice leaves the server in the same state as sending it once.
  • Cookie: a small value the server asks the browser to store and send back on later requests.
  • Stateless: the server keeps no memory of earlier requests unless you add it.

Why it exists

A browser and a server are separate programs. They need one shared format for asking and answering, so a server can tell a page request from an image request. HTTP is that format, and because every message looks the same, one browser can talk to any server.

The picture

HTTP message: a request above a response, each made of start line, headers, blank line and body

Both messages have the same four parts: a start line, headers, a blank line, and an optional body. The blank line is what tells the reader that the headers are finished.

How it works, with an example

Simply: Asking over HTTP is like sending a paper order slip. The browser writes a request with a method, a path and headers, and the server writes back a status such as 200 OK with the answer.

What to know: Your shop page on shop.com runs fetch("https://api.com/cart").

  1. The browser writes the request line: GET /cart HTTP/1.1. The method is GET, the path is /cart.
  2. It adds headers, such as Host: api.com and Origin: https://shop.com, then a blank line. A GET has no body.
  3. The server reads the start line and headers, then runs its code.
  4. The server replies with a status line: HTTP/1.1 200 OK. Then headers such as Content-Type: application/json and Content-Length: 15.
  5. It sends a blank line, then the body: {"items": [42]}. Content-Length says the body is 15 bytes, so the browser knows where it ends.

The most common mistake

Using GET to change data

GET is meant to be safe. Browsers, caches and link previewers may repeat or prefetch a GET. A GET like /cart/delete?id=42 can delete an item by accident. Use POST, PUT, PATCH or DELETE for changes.

Methods

Simply: A method is the verb of the request, like GET to read or POST to create. A safe method changes nothing. An idempotent method has the same effect on the server whether you send it once or many times, like a lift button that still calls one lift.

What to know: Two properties matter:

MethodPurposeSafeIdempotentTypical use
GETReadYesYesLoad /cart
HEADSame as GET, but no body comes backYesYesCheck a size without downloading
OPTIONSAsk what the server allowsYesYesBrowser CORS check
POSTCreate, or do an actionNoNoPlace an order
PUTReplace the whole resourceNoYesSave a whole profile
PATCHChange part of a resourceNoNot guaranteedChange one field
DELETERemoveNoYesDelete a comment
  • GET vs POST: GET reads and its details travel in the URL. POST sends data in the body and usually creates something. A POST sent twice may create two orders. A GET sent twice just reads twice.
  • PUT vs PATCH: PUT sends the full new version. PATCH sends only the fields to change.

Status codes

Simply: Every reply has a three-digit status code, like a note from a kitchen. The first digit is the family: 2 means it worked, 3 means go elsewhere, 4 means your request is wrong, and 5 means the server failed.

What to know:

FamilyMeaningExamples
1xxInformation, more is coming100 Continue
2xxIt worked200 OK, 201 Created, 204 No Content
3xxGo somewhere else or use your copy301, 302, 304
4xxYour request is wrong or not allowed400, 401, 403, 404, 409, 429
5xxThe server failed500, 502, 503

Common codes:

CodeMeaning
200OK, the body holds the answer
201Created, a new item was made
204Worked, and there is no body on purpose
301Moved permanently to the Location URL
302Moved temporarily to the Location URL
304Not modified, use your saved copy
400Bad request, the message is malformed
401Not logged in: no valid credentials
403Logged in, but not allowed
404No resource at this URL
409Conflict with current state, e.g. the name is taken
429Too many requests, slow down
500Generic server bug
502A proxy got a bad answer from the server behind it
503Overloaded or down for maintenance

401 vs 403: 401 means “who are you?” The fix is to log in. 403 means “I know who you are, and you still cannot do this.” Logging in again does not fix a 403. A different permission does.

Stateless and cookies

Simply: The server forgets each request once it answers, so sites use a cookie to remember you. A cookie is a small value the browser stores and sends back each time, like a coat-check ticket.

What to know: The server sends Set-Cookie: session=abc123 in a response. The browser stores it, then sends Cookie: session=abc123 with each later request to that site. The server reads the cookie and finds your session. Cookies are the memory.

Say it in an interview

“An HTTP message has a start line, headers, a blank line, and an optional body. A request’s start line holds the method, path and version. A response’s start line holds the status code. GET reads, POST creates or acts, PUT replaces, and PATCH changes part of a resource. Safe methods do not change server state. Idempotent methods give the same state when repeated. GET, PUT and DELETE are idempotent, POST is not. Status codes group into five families, where 4xx means the request is wrong and 5xx means the server failed. HTTP is stateless, so cookies carry the login between requests.”

Questions interviewers ask

Tap a question to see the answer.

Mental map

HTTP message mental map

Watch

KobbyKernel, about 14 minutes: what a request and a response are made of, and persistent vs non-persistent connections.

Want more? The detailed HTTP message page has the raw request and response, every header that matters, exact rules for safe and idempotent methods, the full status code table, and harder questions.


← Networking quick revision · Request lifecycle →