The HTTP protocol: requests, responses and status codes

Lesson 4 of 8 · Web basics

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.

Before you start: required prior knowledge – web server, how the internet works.
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

MethodMeaningExample
GETread / fetchopening a page, a search
POSTsend new datasending a form, registration, a new comment
PUT / PATCHchange existing data (all / part)editing a profile
DELETEdeletedeleting a message
HEADlike GET, but without a body – only headerschecking 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

GroupMeaningMost common
1xxinformation101 Switching Protocols (switching to WebSocket)
2xxsuccess200 OK, 201 Created, 204 No Content
3xxredirection301 Moved Permanently (the old address permanently leads to the new one), 302 Found, 304 Not Modified (use the copy from the cache)
4xxclient error400 Bad Request, 401 Unauthorized (you are not signed in), 403 Forbidden (you have no permission), 404 Not Found, 429 Too Many Requests
5xxserver error500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable
How to remember: 2 – "everything is fine", 3 – "go over there", 4 – "you made a mistake", 5 – "I (the server) made a mistake".

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?

  1. After a successful sign-in the server sends the header Set-Cookie: session=a8f3… in the response.
  2. The browser saves the cookie and with every following request to the same site sends Cookie: session=a8f3….
  3. 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
HTTPSany 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).

  1. Open F12 → Network, then open a page that does not exist, e.g. /page_that_does_not_exist. What is the status?
  2. Click the request and open Headers: find Request Method, Status Code, Content-Type and User-Agent.
  3. 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.

Common mistakes and tips
  • 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?