# curl basics: fetch a page, save it, read the status code

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/curl-basics-full-en
curl transfers data over HTTP; it exits with 0 when the transfer completes, even for a 404, unless `--fail` is used.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-26-A, TE-26-B and TE-26-C. The expected non-zero exit is TE-26-C (exit status 7). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-26-A: curl exits 0 on an HTTP 404 unless --fail is used

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
mkdir -p www; printf 'ok\n' > www/index.html; nohup python3 -m http.server 8765 --bind 127.0.0.1 --directory www >srv.log 2>&1 & echo $! > srv.pid; sleep 1
```

Command:

```bash
code=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8765/missing.txt); echo code=$code; curl -s -o /dev/null http://127.0.0.1:8765/missing.txt; echo nofail=$?; curl -s -f -o /dev/null http://127.0.0.1:8765/missing.txt; echo withfail=$?; [ -f srv.pid ] && kill $(cat srv.pid) 2>/dev/null; rm -f srv.pid; echo server=127.0.0.1:8765
```

Recorded output:

```
code=404
nofail=0
withfail=22
server=127.0.0.1:8765
```

Exit status: 0. This is the status recorded in the log for TE-26-A.

### TE-26-B: curl prints the body of a page, and -o with -w '%{size_download}' saves it and reports its size

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
mkdir -p www; printf 'ok\n' > www/index.html; nohup python3 -m http.server 8765 --bind 127.0.0.1 --directory www >srv.log 2>&1 & echo $! > srv.pid; sleep 1
```

Command:

```bash
curl -s http://127.0.0.1:8765/index.html; curl -s -o out.txt -w '%{size_download}\n' http://127.0.0.1:8765/index.html; cat out.txt; [ -f srv.pid ] && kill $(cat srv.pid) 2>/dev/null; rm -f srv.pid; echo server=127.0.0.1:8765
```

Recorded output:

```
ok
3
ok
server=127.0.0.1:8765
```

Exit status: 0. This status and the output match what was written before the run.

### TE-26-C: expected non-zero exit: curl exits with status 7 when nothing listens on the address

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
curl -sS http://127.0.0.1:1/
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
curl: (7) Failed to connect to 127.0.0.1 port 1 after 0 ms: Connection refused
```

Exit status: 7. This status and the output match what was written before the run.

TE-26-B: by the setup, www/index.html holds the line ok. The first curl printed ok. The second curl saved the page to out.txt and printed 3 for the size in bytes, and cat out.txt printed ok. TE-26-C: by the setup, no server was started. curl -sS to port 1 on 127.0.0.1 exited with status 7, and standard error reads curl: (7) Failed to connect to 127.0.0.1 port 1 after 0 ms: Connection refused. The number of milliseconds can differ between runs.

## Limits

The example uses a loopback server; start, port and stop are recorded.

## Source

- https://curl.se/docs/manpage.html

## Evidence

- `TE-26-A`: curl exits 0 on an HTTP 404 unless --fail is used

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-26-A` | curl exits 0 on an HTTP 404 unless --fail is used | 0 | PASS | `97bbe015a35b` |
| `TE-26-B` | curl prints the body of a page, and -o with -w '%{size_download}' saves it and reports its size | 0 | PASS | `fa13f46da9c3` |
| `TE-26-C` | expected non-zero exit: curl exits with status 7 when nothing listens on the address | 7 | PASS | `6959b9c1c1eb` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Pipes and redirection: where command output goes](https://kairos-2z6s.onrender.com/res/pipes-redirection-full-en)
- [tar basics: pack, list and unpack a folder](https://kairos-2z6s.onrender.com/res/tar-basics-full-en)
- [chmod: read the permission string and change it](https://kairos-2z6s.onrender.com/res/chmod-basics-full-en)
- [Deleting .env in a new commit does not hide the key](https://kairos-2z6s.onrender.com/res/deleted-env-still-in-history-en)
- [Create and switch to a new Git branch](https://kairos-2z6s.onrender.com/res/create-and-switch-git-branch-en)
- [diff: see what changed between two files](https://kairos-2z6s.onrender.com/res/diff-basics-full-en)
- [xargs: turn lines of input into command arguments](https://kairos-2z6s.onrender.com/res/xargs-basics-full-en)
