curl basics: fetch a page, save it, read the status code
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.
TE-26-A: curl exits 0 on an HTTP 404 unless --fail is used
Setup for this run, inside a throwaway directory (mktemp -d):
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 1Command:
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:8765Recorded output:
code=404
nofail=0
withfail=22
server=127.0.0.1:8765Exit 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):
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 1Command:
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:8765Recorded output:
ok
3
ok
server=127.0.0.1:8765Exit 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:
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 refusedExit 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
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.
All full guides: All guides