Fetching with curl and wget
Fetch URLs from the shell with curl: quiet mode, saving files, headers, status codes, -f for scripts, redirects. Why a 404 still exits 0. wget compared.
Pipes and pipelinesRedirectionExit status and && / || listsdiff and patch
Fetching with curl and wget
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- curl URL prints the response body to standard output, and prints a progress meter to standard error when the body goes to a file. Use -s to silence it and -S to keep error messages.
- A 404 or 500 is a successful transfer as far as curl is concerned: it exits 0. Add -f (--fail) to get exit status 22 on HTTP errors, which scripts need.
- -o file saves under a name you choose, -O uses the remote name, -I shows headers only, -L follows redirects, and -w prints facts such as %{http_code}.
- Curl exit codes describe the transfer, not the page: 7 is connection refused, 22 is an HTTP error under -f. Check them rather than parsing text.
- wget is a download-oriented alternative that saves to a file by default and can mirror sites. It is not installed on macOS by default.
Why a command-line client
Browsers are good at pages. Scripts need the raw response: download a file, call an API, check that a deploy is up, read the headers. A browser cannot be put in a pipeline. curl can. It speaks HTTP (and many other protocols), writes the result to standard output, and exits with a status you can test. That fits straight into pipes.
To keep this honest and offline, I tested against a local server: python3 -m http.server bound to 127.0.0.1 on a spare port, serving a folder with index.html, data.csv and api.json, and killed afterwards. curl was 8.7.1, the macOS system one.
Getting a body
$ curl http://127.0.0.1:18765/api.json
{"ok":true,"n":3}
Curl showed a progress table first, on standard error. It does that when the body is not going to a terminal, and once that table is visible in a pipeline it is just noise. -s silences it:
$ curl -s http://127.0.0.1:18765/api.json | jq .n
3
-s also hides error messages. Add -S (so -sS) to keep errors:
$ curl -sS http://127.0.0.1:1/
curl: (7) Failed to connect to 127.0.0.1 port 1 after 0 ms: Couldn't connect to server
Saving: -o name picks a filename and -O uses the one in the URL.
$ curl -s http://127.0.0.1:18765/data.csv -o got.csv
$ curl -sO http://127.0.0.1:18765/data.csv
$ ls
data.csv got.csv
Headers and facts
-I sends a HEAD request and prints only the headers:
$ curl -sI http://127.0.0.1:18765/index.html
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.9.6
Content-type: text/html
Content-Length: 15
...
-v shows both directions. Filtering to lines that begin with > (what curl sent) or < (what came back) is a quick way to read it:
$ curl -sv http://127.0.0.1:18765/index.html -o /dev/null 2>&1 | grep -E '^[<>]' | head -4
> GET /index.html HTTP/1.1
> Host: 127.0.0.1:18765
> User-Agent: curl/8.7.1
> Accept: */*
The verbose text goes to standard error, hence 2>&1, from the streams lesson. For one fact, use -w with a format. Throw the body away and print only what you asked for:
$ curl -s -o /dev/null -w 'status=%{http_code} size=%{size_download} type=%{content_type}\n' http://127.0.0.1:18765/api.json
status=200 size=18 type=application/json
The part that bites in scripts
An HTTP error is still a completed transfer, so by default curl does not treat it as failure:
$ curl -s -o /dev/null http://127.0.0.1:18766/nope; echo "plain curl on 404: exit=$?"
plain curl on 404: exit=0
$ curl -sf -o /dev/null http://127.0.0.1:18766/nope; echo "curl -f on 404: exit=$?"
curl -f on 404: exit=22
A script that downloads with plain curl -s > file will happily save the server’s HTML error page and carry on. With -f the status becomes 22 and the usual || handling from the exit status lesson works. A useful default for scripts is curl -fsS: fail on HTTP errors, no progress, but show real error messages. Connection problems have their own codes, such as 7 for refused, which I got above.
Redirects
Asking for a directory without a trailing slash made my server answer with a 301:
$ curl -sI http://127.0.0.1:18765/docs | head -2
HTTP/1.0 301 Moved Permanently
Server: SimpleHTTP/0.6 Python/3.9.6
$ curl -sIL http://127.0.0.1:18765/docs | grep -E 'HTTP|Location'
HTTP/1.0 301 Moved Permanently
Location: /docs/
HTTP/1.0 200 OK
Curl does not follow redirects unless you pass -L. Download links on release pages are usually redirects, so -L is why curl -O sometimes saves a tiny file that says “Moved”.
Sending data, and the limits of this test
-d 'a=1' sends a POST with that body, and -H 'Content-Type: application/json' sets a header for JSON APIs. My test server is a file server and does not accept POST, so it answered 501, which shows the request was sent but not that any server handled it:
$ curl -s -o /dev/null -w '%{http_code}\n' -X POST -d 'a=1' http://127.0.0.1:18766/
501
I also tried -r 0-3 (a byte range) and -C - (resume), but the server ignored the range and sent the whole file, so I cannot show either working. Both need a server that supports range requests. I did not call any external site, and the TLS side of curl (certificates, -k) was not exercised.
wget
wget does the same job with different defaults: it saves to a file named from the URL without being asked, retries on its own, and can follow links to mirror a site recursively. Curl is the tool for requests and pipelines, wget for plain downloads. command -v wget printed nothing on this Mac, so wget is not installed by default and I could not run any of it. Install it with a package manager and see man wget.
About the video
The embed is a clip, from 1:03 to 15:12, of “cURL Tutorial for Beginners | Test APIs & Web Requests from the Command Line”, a 26-minute video from it-bs media published January 2026. Its chapters are: introduction, understanding curl (from 1:03), basic GET requests, verbose debugging, sending POST requests (to 11:17), then authentication, authorization tokens, header manipulation, file uploads and a summary of flags. The clip keeps the first four chapters on what curl does, GET, -v debugging and POST, and leaves out authentication and uploads. I could not read a transcript, so I do not know whether it mentions -f or exit codes, and it is a small channel. It is in English and covers curl only. The wget video and a talk by curl’s lead developer, Daniel Stenberg, are linked under deeper.
Where this fits
curl gets text from a server, and much of what servers return is JSON. The next lesson is how to read it in a pipeline: jq.
Check yourself
A script runs curl -s https://host/file > out and carries on after the server returned 404. Why, and what is the fix?
curl exits 0 for any completed HTTP exchange. Add -f so HTTP errors give exit status 22, and check the status, for example with curl -fsS ... || exit 1.
What is the difference between -o and -O?
-o name saves the body to a name you pick. -O saves it under the last part of the URL.
How do you print just the HTTP status code of a URL?
curl -s -o /dev/null -w '%{http_code}\n' URL. The body is discarded and -w prints the code.
curl -I returns a 301 and no content. What do you add to see the page the redirect points to?
-L to follow redirects. -I alone only shows the redirect response and its Location header.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.