What happens when you tap a link
You tap a link to a game's patch notes. Less than a second later, the page is on your screen.
In that second your phone asked another computer for the page, and that computer answered. Today you watch that conversation happen, one line at a time.
By the end of this lesson
Note
I can follow a web page from the request to the reply.
Key words
| Word | What it means | Example |
|---|---|---|
| Web server | a program that hands out web pages | |
| Request | what a browser sends to ask for something | |
| Reply | what the server sends back, also called a response | |
| HTTP | the rules a browser and a server follow to talk | |
| Status code | a three-digit number that says how a request went | 404 |
| Header | a line of extra information about a request or a reply | Server: nginx |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top, then wait until it says it is running. The lab is a small bakery website, and your terminal reaches it by the name web. This copy is yours alone and has no way out to the internet. Only send requests to your own copy.
A browser asks and a server answers
A web page lives on a web server. Your browser sends it a request, and the server sends back a reply. Both follow the same rules, called HTTP.
The address of a page is its URL, such as http://web/files/. In the lab you use curl, a program that sends a request and prints the reply as text.
Ask only for the reply's headers
curl -s -I http://webShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Reading the headers
-I asks for the headers and not the page. The first line, HTTP/1.1 200 OK, says the request worked.
Each line after it is a header. Server: nginx/1.27.5 names the server program. Content-Type: text/html says the reply is a web page. Content-Length: 145 says the page is 145 bytes long.
Every reply starts with a status line
The first line of a reply is the status line. It has three parts, split by spaces: the version of HTTP, the status code and a few words for people.
A program only needs the code. The words can be more than one word long, so a program splits the line at the first two spaces only.
Worked example: splitting a status line
| Status line | Version | Code | Words |
|---|---|---|---|
| HTTP/1.1 200 OK | HTTP/1.1 | 200 | OK |
| HTTP/1.1 404 Not Found | HTTP/1.1 | 404 | Not Found |
| HTTP/1.1 405 Not Allowed | HTTP/1.1 | 405 | Not Allowed |
The cells students fill in are left blank in this preview.
Ask for a page that is not there
curl -s -I http://web/nothing-here | head -n 1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Status codes come in groups
| First digit | Meaning | Example |
|---|---|---|
| 2 | It worked | 200 OK |
| 3 | It has moved: look somewhere else | 301 Moved Permanently |
| 4 | The request was wrong | 404 Not Found, 405 Not Allowed |
| 5 | The server went wrong | 500 Internal Server Error |
The cells students fill in are left blank in this preview.
Ask the server to delete its home page
curl -s -I -X DELETE http://web/ | head -n 1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think a 404 means the website is down. A 404 is an answer, so the server is working. It just has no page at that address. When a server is really down, no status code comes back at all.
Going further: a page that moved
Type curl -s -I http://web/files | head -n 1 in the terminal. There is no slash at the end of the address. Which group is the code in?
Then type curl -s -I http://web/files | grep -i "^location". That header tells the browser where to go instead. Why do you think the server sent it?