Retrieving content
On this page
You need a text file from a server, but your terminal shows either a page full of markup or a progress report. Before saving anything, you need to distinguish the server’s content from information about the transfer.
By the end, you can fetch a page, inspect its response headers, save its body under a chosen local name, and read that saved file. You can also recognize an HTTP failure without mistaking it for a failed network path.
Before you start
Section titled “Before you start”Finish Inspecting local connectivity. Use ~/lab from the B04 setup. Reserve the absent directory ~/lab/b14-fetch for this exercise. If you already used it, finish the reset before repeating the steps.
The recording uses the lab server, labserver, on port 80. This is a private Docker bridge between two containers, not a home network or an internet fetch. The hostname works inside that prepared lab. On your own VM, use a web server you control instead; the name labserver will not exist there.
Your addresses, HTTP Date headers, timestamps, and transfer speeds will differ. The capture preserves them as observed. They describe the container network, not your physical computer’s internet speed.
Request a page body
Section titled “Request a page body”Prepare an empty local directory:
mkdir ~/lab/b14-fetchcd ~/lab/b14-fetchRequest the lab page:
curl -sS http://labserver/A URL identifies the resource to request. Here, http selects the protocol, labserver names the host, and / is the path. HTTP uses port 80 by default for this URL. The server returns its index page.
curl transfers data. By default, it writes the response body to standard output, which your terminal displays. The lowercase -s suppresses the progress meter; uppercase -S keeps error messages visible when silent mode is enabled. These options make the body easier to read without hiding a failed request.
The body is the requested content. For this page it is HTML source: text containing tags such as h1 and p. Curl does not render a graphical web page. Seeing markup is expected, rather than evidence that the download is corrupt.
Read headers separately
Section titled “Read headers separately”curl -sS -I http://labserver/hello.txtThe uppercase -I requests headers using HTTP’s HEAD method. Headers describe the response; they are separate from the body. This request does not print the text file’s contents.
Predict: will this command print the greeting from the text file after its header lines?
Run it, then compare with your guess
learner@rootvolt:~/lab/b14-fetch$ curl -sS http://labserver/<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>RootVolt lab server</title></head>
<body>
<h1>RootVolt lab server</h1>
<p>This page is served by the lab server on port 80.</p>
</body>
</html>learner@rootvolt:~/lab/b14-fetch$ curl -sS -I http://labserver/hello.txtHTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.3
Date: Sun, 15 Nov 2026 16:07:56 GMT
Content-type: text/plain
Content-Length: 73
Last-Modified: Sun, 15 Nov 2026 00:00:00 GMTlearner@rootvolt:~/lab/b14-fetch$ Steps (6)
- Type
curl -sS http://labserver/ - Press Enter
- Output:
<!doctype html> <html lang="en"> <head><meta charset="utf-8"><title>RootVolt lab server</title></head> <body> <h1>RootVolt lab server</h1> <p>This page is served by the lab server on port 80.</p> </body> </html> - Type
curl -sS -I http://labserver/hello.txt - Press Enter
- Output:
HTTP/1.0 200 OK Server: SimpleHTTP/0.6 Python/3.12.3 Date: Sun, 15 Nov 2026 16:07:56 GMT Content-type: text/plain Content-Length: 73 Last-Modified: Sun, 15 Nov 2026 00:00:00 GMT
No. The headers-only response ends after its metadata. The earlier HTML body belongs to a different request, for the root page.
On a phone, long header lines can wrap. Read the key fields individually:
| Response field | Captured meaning |
|---|---|
HTTP/1.0 200 OK |
The server reports success for this request |
Content-type: text/plain |
The resource is plain text |
Content-Length: 73 |
The reported body length is 73 bytes |
Date |
The server’s response time; it varies |
Last-Modified |
The resource’s modification timestamp, separate from response time |
The first line is the HTTP status line. 200 is a protocol result, not a shell exit status. The shell separately reports whether curl completed successfully. The lab uses a small Python HTTP server; your other servers can report different versions, headers, and capitalization.
Save the body with curl
Section titled “Save the body with curl”curl -sS -o curl.txt http://labserver/hello.txtcat curl.txtLowercase -o takes a local output pathname. It writes the body to curl.txt in your current directory instead of printing it. This command does not include the header lines in the file.
The successful save is quiet because you suppressed the progress meter and redirected the body’s destination with curl’s own option. Silence alone is not the content check. cat reads back the actual saved bytes, letting you confirm that the file contains the greeting.
The remote path is /hello.txt; the local path is ~/lab/b14-fetch/curl.txt. These names need not match. The URL identifies what to retrieve, and the option identifies where to store it locally.
Save the same resource with wget
Section titled “Save the same resource with wget”wget -O wget.txt http://labserver/hello.txtcat wget.txtFor wget, uppercase -O chooses the destination file. The letter’s case matters: curl’s lowercase -o and wget’s uppercase -O are different programs’ options. Wget normally saves a resource to a file and prints a transfer report. Here, choosing a separate name makes the two results easy to inspect.
learner@rootvolt:~/lab/b14-fetch$ curl -sS -o curl.txt http://labserver/hello.txtlearner@rootvolt:~/lab/b14-fetch$ cat curl.txtHello from the RootVolt lab server.
This text file is served on port 80.learner@rootvolt:~/lab/b14-fetch$ wget -O wget.txt http://labserver/hello.txt--2026-11-15 16:07:58-- http://labserver/hello.txt
Resolving labserver (labserver)... 172.19.0.2
Connecting to labserver (labserver)|172.19.0.2|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 73 [text/plain]
Saving to: ‘wget.txt’
wget.txt 100%[===================>] 73 --.-KB/s in 0s
2026-11-15 16:07:58 (25.8 MB/s) - ‘wget.txt’ saved [73/73]learner@rootvolt:~/lab/b14-fetch$ cat wget.txtHello from the RootVolt lab server.
This text file is served on port 80.learner@rootvolt:~/lab/b14-fetch$ Steps (11)
- Type
curl -sS -o curl.txt http://labserver/hello.txt - Press Enter
- Type
cat curl.txt - Press Enter
- Output:
Hello from the RootVolt lab server. This text file is served on port 80. - Type
wget -O wget.txt http://labserver/hello.txt - Press Enter
- Output:
--2026-11-15 16:07:58-- http://labserver/hello.txt Resolving labserver (labserver)... 172.19.0.2 Connecting to labserver (labserver)|172.19.0.2|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 73 [text/plain] Saving to: ‘wget.txt’ wget.txt 100%[===================>] 73 --.-KB/s in 0s 2026-11-15 16:07:58 (25.8 MB/s) - ‘wget.txt’ saved [73/73] - Type
cat wget.txt - Press Enter
- Output:
Hello from the RootVolt lab server. This text file is served on port 80.
Wget’s report includes the resolved address, port 80, HTTP status 200 OK, and saved size [73/73] in this capture. The content appears when cat reads the file. The transfer report itself is not part of wget.txt.
Both destination options can replace existing files. Use this fresh exercise directory, and do not choose an existing document as the destination. Neither tool needs administrative privileges to write here.
Recognize an HTTP error
Section titled “Recognize an HTTP error”Request a deliberately absent resource:
curl -sS --fail http://labserver/missing.txtThe reset replay below includes the recorded error: curl: (22) The requested URL returned error: 404. The --fail option makes this HTTP failure produce a nonzero curl status, 22 here, and suppresses the error response body.
A 404 means an HTTP server answered but did not find that resource. It is different from failing to resolve the hostname or open the connection. Without --fail, curl can successfully transfer an HTTP error page and still report transfer success. Do not treat a saved file’s mere existence as evidence that it is the intended resource.
Mistakes to recognize
Section titled “Mistakes to recognize”Saving the headers when you wanted the body. -I asks for headers only. Remove that option for an ordinary body request.
Expecting curl to display a rendered web page. It prints the body bytes. HTML source is the body for the lab’s root page.
Calling every failure “offline.” The recorded 404 is a response from a reachable HTTP server. Inspect the URL path before changing network settings.
Try it yourself
Section titled “Try it yourself”Goal: Explain which file would change if you chose note.txt as the local output name while keeping the URL /hello.txt unchanged. Would the server file be renamed?
Solution
The downloaded body would be written to local note.txt. The server still serves /hello.txt; the local destination option does not rename or modify that remote resource. Check the saved content before using it.
Reset this lesson
Section titled “Reset this lesson”Remove only the two known local downloads, leave the directory, and remove it while empty:
rm curl.txt wget.txtcd ~/labrmdir ~/lab/b14-fetchtest ! -e ~/lab/b14-fetch && printf 'Reset complete\n'learner@rootvolt:~/lab/b14-fetch$ curl -sS --fail http://labserver/missing.txtcurl: (22) The requested URL returned error: 404learner@rootvolt:~/lab/b14-fetch$ rm curl.txt wget.txtlearner@rootvolt:~/lab/b14-fetch$ cd ~/lablearner@rootvolt:~/lab$ rmdir ~/lab/b14-fetchlearner@rootvolt:~/lab$ test ! -e ~/lab/b14-fetch && printf 'Reset complete\n'Reset completelearner@rootvolt:~/lab$ Steps (12)
- Type
curl -sS --fail http://labserver/missing.txt - Press Enter
- Output:
curl: (22) The requested URL returned error: 404 - Type
rm curl.txt wget.txt - Press Enter
- Type
cd ~/lab - Press Enter
- Type
rmdir ~/lab/b14-fetch - Press Enter
- Type
test ! -e ~/lab/b14-fetch && printf 'Reset complete\n' - Press Enter
- Output:
Reset complete
The server’s fixed files were only read. There are no remote changes to undo. Successful rmdir and the final absence test establish the local reset.
- A body contains resource data; headers describe the response.
- Curl normally prints the body;
-Irequests only headers. - Choose local destinations with curl
-oor wget-O, then read the saved content. - An HTTP error and a shell exit status are separate results.
Next, First SSH connection verifies the server identity before opening a remote shell.
Further reading
Section titled “Further reading”- On Ubuntu:
man curlandman wget. - curl manual, especially
--head,--output,--silent,--show-error, and--fail. - GNU Wget manual: Download options.