curl & wget
curl and wget transfer data over the network from the command line: downloading files, calling REST APIs, checking that a website answers, installing software. wget is built for downloading (files, whole sites, resuming broken downloads); curl is a Swiss-army knife for HTTP requests of every kind.
Note: The machine that runs this course's examples has no network access, so the network commands below are shown with sample output. The runnable examples work with responses saved to files, which is also how you'd test a script that processes API data.
Downloading a file
wget https://example.com/files/dataset.csv # saves dataset.csv
wget -O data.csv https://example.com/files/dataset.csv # choose the name
wget -c https://example.com/big.iso # continue an interrupted download
curl -O https://example.com/files/dataset.csv # -O: keep the remote name
curl -o data.csv https://example.com/files/dataset.csv # -o: choose the name
curl -L -o app.tar.gz https://example.com/latest # -L: follow redirectsThe biggest gotcha: plain curl URL prints the response to the screen instead of saving it, and it doesn't follow redirects unless you add -L.
Making HTTP requests with curl
| Option | Meaning |
|---|---|
-X POST |
HTTP method (GET is the default) |
-H 'Name: value' |
Add a request header |
-d 'data' |
Send a request body (implies POST) |
--json '{...}' |
Send JSON with the right headers (curl 7.82+) |
-i / -I |
Include response headers / fetch headers only |
-s / -S |
Silent (no progress bar) / still show errors |
-f |
Fail with an error status on HTTP 4xx/5xx |
-w '%{http_code}' |
Print details such as the status code after the transfer |
-u user:pass |
Basic authentication |
curl -s https://api.example.com/users/42
curl -s -X POST https://api.example.com/users \
-H 'Content-Type: application/json' \
-d '{"name": "Ada", "role": "admin"}'
curl -sI https://example.comSample output:
{"id": 42, "name": "Grace", "role": "editor", "active": true}
{"id": 43, "name": "Ada", "role": "admin", "active": true}
HTTP/2 200
content-type: text/html; charset=UTF-8
content-length: 1256Processing a JSON response
APIs answer in JSON, and jq is the standard tool for picking it apart. In a script you'd write curl -s URL | jq ...; here the response is saved to a file first:
cat > users.json <<'JSON'
{"page": 1, "users": [
{"id": 1, "name": "Ada", "role": "admin", "active": true},
{"id": 2, "name": "Linus", "role": "dev", "active": false},
{"id": 3, "name": "Grace", "role": "dev", "active": true}
]}
JSON
jq -r '.users[] | select(.active) | "\(.id) \(.name) (\(.role))"' users.json
jq '.users | length' users.json
jq -r '[.users[].role] | unique | join(", ")' users.json1 Ada (admin)
3 Grace (dev)
3
admin, dev
| jq expression | Meaning |
|---|---|
.users |
The users field |
.users[] |
Each element of the array |
select(.active) |
Keep elements where active is true |
"\(.name)" |
String interpolation |
-r |
Raw output: strings without quotes |
Checking status codes in scripts
A health check typically asks only for the status code: curl -s -o /dev/null -w '%{http_code}' URL. The decision logic around it is ordinary Bash, so we can test it with saved codes:
check() {
local name=$1 code=$2
case $code in
2??) echo "OK $name ($code)" ;;
3??) echo "MOVED $name ($code)" ;;
000) echo "DOWN $name (no connection)" ;;
*) echo "FAIL $name ($code)" ;;
esac
}
# in real life: code=$(curl -s -o /dev/null -w '%{http_code}' "$url")
check homepage 200
check old-blog 301
check api 503
check intranet 000OK homepage (200)
MOVED old-blog (301)
FAIL api (503)
DOWN intranet (no connection)
curl prints 000 when it couldn't connect at all. Add --max-time 10 so a hanging server can't block your script forever, and --retry 3 for flaky networks.
What happens without a network
Network commands fail with a clear error and a non-zero exit status, which scripts should check. Here the small BusyBox wget installed in the sandbox tries a host that can't be reached (GNU wget words its error differently, but fails the same way):
wget -q -O page.html http://example.com/ || echo "download failed with status $?"
[ -s page.html ] || echo "page.html is empty or missing"wget: bad address 'example.com'
download failed with status 1
page.html is empty or missing
[ -s FILE ] tests that a file exists and is not empty. Always check that a download succeeded before using the file.
The "curl | bash" pattern
Many installers say curl -fsSL https://get.example.com | bash. It's convenient, but it runs code from the internet with your privileges without letting you read it first. Safer: download to a file, read it, then run it.
Test yourself
Which command saves https://x.org/a.zip as a.zip with curl?
- curl https://x.org/a.zip
- curl -O https://x.org/a.zip
- curl -I https://x.org/a.zip
- curl -d https://x.org/a.zip
Which curl option follows HTTP redirects?
- -f
- -L
- -s
- -r
What does jq -r '.users[].name' output?
- The JSON array of users
- Each user's name, one per line, without quotes
- The number of users
- An error