The HTTP protocol: requests, responses and status codes
HTTP is the "language" in which the browser and the server talk. Every request and every response has the same shape: a start line, headers and (sometimes) a body. Once you understand it, you can easily follow what any web application does – and find errors quickly.
How to learn: try every example in the browser or on the command line and do the exercises from the lesson. The examples are in HTML, JavaScript and Python; for most of them a browser is enough, and for the Python examples you need Python 3 installed.
What an HTTP request looks like
GET /binary_search HTTP/1.1 ← start line: method, path, version
Host: svetprogramiranja.com ← headers (name: value)
User-Agent: Mozilla/5.0 (Windows NT 10.0) Chrome/130
Accept: text/html
Accept-Language: en,sr
← empty line: end of the headers
A GET request has no body. When data is sent (a form, a sign-in), POST is used and the data goes into the body after the empty line:
POST /login HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 31
username=ana&password=secret123
HTTP methods
| Method | Meaning | Example |
|---|---|---|
| GET | read / fetch | opening a page, a search |
| POST | send new data | sending a form, registration, a new comment |
| PUT / PATCH | change existing data (all / part) | editing a profile |
| DELETE | delete | deleting a message |
| HEAD | like GET, but without a body – only headers | checking whether a page exists |
GET parameters are visible in the address (?q=loops) and stay in the browser history, which is why passwords are never sent with GET.
What an HTTP response looks like
HTTP/1.1 200 OK ← version, status code, description
Content-Type: text/html; charset=utf-8 ← kind of content
Content-Length: 18240
Cache-Control: max-age=3600 ← the browser may keep a copy for 1 hour
<!DOCTYPE html> ← body: an HTML page
<html lang="en"> ...
The Content-Type header tells the browser what it has received: text/html, text/css, application/javascript, image/png, application/json…
Status codes
| Group | Meaning | Most common |
|---|---|---|
| 1xx | information | 101 Switching Protocols (switching to WebSocket) |
| 2xx | success | 200 OK, 201 Created, 204 No Content |
| 3xx | redirection | 301 Moved Permanently (the old address permanently leads to the new one), 302 Found, 304 Not Modified (use the copy from the cache) |
| 4xx | client error | 400 Bad Request, 401 Unauthorized (you are not signed in), 403 Forbidden (you have no permission), 404 Not Found, 429 Too Many Requests |
| 5xx | server error | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable |
HTTP has no memory: cookies and sessions
HTTP is stateless: the server does not remember the previous request. So how do you stay signed in while you move from page to page?
- After a successful sign-in the server sends the header
Set-Cookie: session=a8f3…in the response. - The browser saves the cookie and with every following request to the same site sends
Cookie: session=a8f3…. - By that token the server recognizes the session – it knows it is you.
That is why a session cookie is as confidential as a password: whoever steals it can pretend to be you. More in the lesson Web application security.
HTTP, HTTPS and protocol versions
| Description | |
|---|---|
| HTTP/1.1 (1997) | text requests; one request per connection at a time |
| HTTP/2 (2015) | several requests at once through one connection, header compression |
| HTTP/3 (2022) | works over the QUIC protocol (UDP) – faster on poor networks and phones |
| HTTPS | any version of HTTP, but encrypted with TLS – the padlock in the address bar |
Today almost all sites use HTTPS, and browsers mark sites without it as "not secure".
Exercise: watch HTTP in the browser and on the command line
The browser's tools (DevTools): in Chrome, Edge and Firefox they open with the F12 key (or right click → Inspect). The most important tabs are Elements (the page's HTML and CSS), Console (JavaScript messages and errors) and Network (all the requests the page sends).
- Open F12 → Network, then open a page that does not exist, e.g.
/page_that_does_not_exist. What is the status? - Click the request and open Headers: find Request Method, Status Code,
Content-TypeandUser-Agent. - On the command line (Windows 10/11 has
curl) type:
curl -I https://svetprogramiranja.com/introduction_to_the_web
The -I option asks only for the headers (HEAD). You will see the status 200 and the response headers. Also try with the address http:// (without the s) – the server answers with a 301 redirect to https.
- Passwords and personal data are sent with POST, and only over HTTPS.
- 404 is an error in the address (client), 500 is an error in the code on the server.
- Headers are written as "Name: value", and one empty line separates them from the body.
Check your knowledge
Open the quiz
The HTTP protocol
1. Which method is used to send a registration form?
2. What does the status code 404 mean?
3. How does the server "remember" that you are signed in, when HTTP has no state?
4. Which group of codes means a server error?
