HTTP, the Hypertext Transfer Protocol, is the language browsers and web servers use to talk to each other. Every web page, image and app screen you load arrives through it. HTTPS is the same language sent through an encrypted connection.
What happens when you open a page
The browser reads the address and splits it into its parts (see below).
DNS turns the name into an IP address (see the guide to DNS).
The browser opens a connection to that address. For
https://it first agrees on encryption with the server.The browser sends a request: "please give me this page".
The server sends a response: a status code, headers and usually the page itself.
The browser reads the page and finds the images, styles and scripts it needs. Each one is another request. A single page can easily need dozens.
The parts of a URL
https://shop.northwind.example:443/products/lamps?colour=blue#reviews
Part | Here | What it is |
|---|---|---|
Scheme |
| Which protocol to use |
Host |
| Which server to ask |
Port |
| Which "door" on the server; usually left out, because 443 is the default for HTTPS and 80 for HTTP |
Path |
| Which resource on the server |
Query |
| Extra details, such as a search or a filter |
Fragment |
| A place on the page; it stays in the browser and is never sent to the server |
Request and response
A browser asks, a server answers. Both messages are plain text with the same shape: a first line, headers, a blank line and an optional body.
GET /index.html HTTP/1.1
Host: example.com
Accept: text/html
User-Agent: Mozilla/5.0HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Cache-Control: max-age=3600
Content-Length: 1256
<!doctype html>
...Read the request line by line: the method GET, the path /index.html and the version HTTP/1.1; then Host names the website (one server often hosts many), Accept says what the browser can handle and User-Agent names the browser. The response starts with the version, the status code 200 and its short text OK, then its headers, a blank line and the HTML.
Methods
The method says what the browser wants to do.
Method | What it does | Example |
|---|---|---|
GET | Read something | Open a page |
POST | Send data for the server to process | Submit a form |
PUT | Replace something with what is sent | Save a whole edited profile |
PATCH | Change part of something | Update only the phone number |
DELETE | Remove something | Delete a comment |
HEAD | Like GET, but only the headers, no body | Check whether a page exists |
Status codes
The three digits sort every response into one of five classes.
Class | Meaning | Examples |
|---|---|---|
1xx | Information, keep going | 100 Continue |
2xx | Success | 200 OK, 201 Created, 204 No Content |
3xx | Look elsewhere | 301 Moved Permanently, 302 Found, 304 Not Modified |
4xx | The request has a problem | 400 Bad Request, 403 Forbidden, 404 Not Found, 429 Too Many Requests |
5xx | The server has a problem | 500 Internal Server Error, 503 Service Unavailable |
A 3xx answer carries a Location header with the new address, and the browser follows it on its own. 301 and 308 mean "moved for good", 302 and 307 mean "for now". 304 is different: it tells the browser that its saved copy is still current, so nothing has to be sent again.
Two pairs that are often mixed up: 401 means "we do not know who you are, please sign in", 403 means "we know who you are, and you may not". 502 and 504 come from a server in the middle (a gateway) that got a bad answer, or no answer in time, from the server behind it.
Worked example: a redirect
Northwind's website moved to HTTPS. A visitor still types the old address.
GET / HTTP/1.1
Host: northwind.example
HTTP/1.1 301 Moved Permanently
Location: https://northwind.example/The browser sees 301, reads Location and sends a second request to the HTTPS address, which answers 200 OK with the page. The visitor only notices that the address bar now shows https://.
Headers worth knowing
content-type: what the body is, such as
text/htmlorapplication/json.cache-control: how long the browser may reuse the response without asking again.
max-age=3600means one hour.location: where a redirect points.
set-cookie: a small value the browser stores and sends back with later requests.
strict-transport-security: tells the browser to use HTTPS for this site from now on.
server: which server software answered, if the site chooses to say.
Header names are not case-sensitive: Content-Type and content-type are the same header. Newer HTTP versions write them in lower case.
Cookies: how a server remembers you
HTTP itself forgets: every request stands on its own. To keep you signed in, the server sends set-cookie with an ID, the browser stores it and sends it back as cookie with every later request to that site. That is why clearing cookies signs you out.
What HTTPS protects, and what it does not
HTTPS encrypts the connection, so nobody on the way (for example on public Wi-Fi) can read or change the pages, passwords or cookies. The padlock in the address bar means exactly that, and no more. It does not mean the site is honest: phishing sites use HTTPS too.
HTTP versions
HTTP/1.1 sends the text you see above. HTTP/2 and HTTP/3 pack the same methods, status codes and headers into a faster binary form. The meaning is the same in all three, which is why tools can show you the same headers whichever version was used.
Common mistakes
"404 means the server is down." A 404 is an answer, so the server is working; it just has nothing at that path. A server that is down gives no answer, or a 5xx code from a gateway in front of it.
"200 means everything is fine." Some sites answer 200 with a "page not found" text. The code says the request worked, not that the content is what you wanted.
Thinking the padlock means "safe". It means "encrypted".
Forgetting the cache. A change on the server may not show because the browser reuses its saved copy until
max-ageruns out.Thinking the fragment reaches the server.
#reviewsnever leaves the browser.
Check yourself
1.Which part of a request says what the browser wants to do?
2.A server answers 404. What does that tell you?
3.Which header tells the browser where a redirect points?
4.What is the difference between 401 and 403?
5.Which part of https://shop.northwind.example/lamps?colour=blue#reviews is never sent to the server?
6.A site shows a padlock in the address bar. What does that prove?
7.Why does clearing cookies usually sign you out of websites?
Exercise
Open the HTTP Header Analyzer and request
https://example.com. Which status code comes back, and what is the content-type?The analyzer follows redirects and shows the final answer. To see a redirect itself, use
curlin a terminal; it is built into Windows 10 and 11, macOS and most Linux systems. Runcurl -I http://followed by your school's domain (in Windows PowerShell, typecurl.exeinstead ofcurl). Is the first answer a 3xx? Which header gives the new address away?Request an address that does not exist on a site you know, such as
/does-not-exist. Which class does the status code belong to?Find one header in a response that is not on the list above and look up what it does.
Notes on the answers
Usually 200 and
text/html, often with a charset such as UTF-8.Most sites answer 301 or 308 and send a
locationheader with thehttps://address. If the answer is 200, the site still serves the page over plain HTTP.Usually 404, a client error (4xx). If a site answers 200 with a "not found" page, that is a good example of the second common mistake above.
Answers vary. Common ones:
date(when the response was made),etag(a fingerprint of the content for caching),vary(which request headers change the answer),x-content-type-options(stops the browser guessing the content type).
Practise the codes with the HTTP status codes exercise.
