curl Command Guide for Transferring Data with URLs
curl talks to servers over URLs — HTTP, HTTPS, FTP, and dozens of other protocols — and can download files, send requests to APIs, or just show you what a server replies.
What is curl, and Why Use It?
curl (“client URL”) is a command-line tool for transferring data with URLs. Point it at an address and it fetches the resource, printing it to your terminal or saving it to a file. It supports more protocols than almost anything else you will meet — HTTP, HTTPS, FTP, FTPS, SFTP, SMTP, IMAP, LDAP, and many more — which makes it the universal client for “ask a server for something”.
It is also the standard tool for working with APIs: you send it headers, request bodies, and authentication, and read back JSON responses. When you read a setup script or follow a web tutorial, curl is very often what you are looking at.
Why Learn curl?
- It is the standard way to talk to HTTP from the shell — APIs, health checks, and download scripts are all written with it.
- It does far more than download — headers, POST requests, redirects, cookies, authentication, and rate-limited retries are all built in.
- It exists almost everywhere — any Linux install has it, and it is the default on macOS too, so scripts you write will run anywhere.
curldoes not write to disk unless you tell it to. By default it prints the response body to your terminal —curl -Oorcurl -ois what makes it download.
1. Fetching a URL
The simplest use — fetch a page and print it:
curl example.com
By default curl writes the response body to standard output, which for a web page is HTML. That is rarely what you want to read in a terminal, so almost every real use adds an option.
A cleaner first command — show only the response headers:
curl -I https://example.com
HTTP/2 200
content-type: text/html; charset=UTF-8
content-length: 1256
server: ECS (nyf/8019)
-I (HEAD) asks for headers only: the status line tells you if the URL works (200 means OK, 404 means not found), and the rest describe the server. This is the quickest way to check whether a site is up or a URL is broken.
2. Downloading Files to Disk
Two ways to save what you fetched:
curl -O https://example.com/files/archive.tar.gz # keep the remote filename
curl -o my-copy.tar.gz https://example.com/files/archive.tar.gz # your filename
-O (capital) uses the name from the URL; -o (lowercase) lets you name the file. To download and follow redirects, add -L — many sites redirect http to https or through a CDN, and without -L you may get an empty file:
curl -L -O https://example.com/latest-release.tar.gz
If
curlprints HTML when you expected a file, the URL was wrong or the server returned an error page. Check the status withcurl -Ifirst.
3. Following Redirects and Speaking HTTP Properly
Real-world HTTP involves status codes, redirects, headers, and methods. curl exposes all of them:
curl -L example.com # follow redirects (301/302) to the final page
curl -v example.com # verbose: show the full request and response
curl -s example.com # silent: progress bar off (for scripts)
-v is the learning tool: it prints the exact request headers curl sends and the exact status line the server returns. When an API or site “isn’t working”, -v shows you what is actually being exchanged.
To send a POST request with data (how you talk to most APIs):
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name": "ada", "role": "admin"}'
-X POST chooses the method, -H sets a header, and -d sends a body. Without -X, adding -d still defaults to POST — the flag is optional but explicit.
4. Working With APIs: Headers, Auth, and JSON
A realistic API call with authentication:
curl -H "Authorization: Bearer YOUR_TOKEN" \
https://api.example.com/v1/items
And to pretty-print JSON (if jq is installed):
curl -s https://api.example.com/v1/items | jq .
Common patterns you will reuse:
-H "Accept: application/json"— ask the server for JSON instead of HTML.-u user:password— HTTP basic auth.--data-urlencode "q=hello world"— URL-encode a query parameter.
5. Uploading and Sending Forms
curl can also send data, which is how you submit forms and upload files from scripts:
# Upload a file with a multipart form POST
curl -F "[email protected]" https://api.example.com/uploads
# Submit a form (application/x-www-form-urlencoded)
curl -d "username=ada&password=secret" https://example.com/login
-F sends multipart form data (the standard way to upload files), and -d sends plain form fields. Both default the method to POST.
6. Piping curl Into Other Commands
Because curl writes to standard output, it composes with everything in the pipes section:
# Show just the HTTP status code of many URLs
for url in $(cat urls.txt); do
echo "$url $(curl -s -o /dev/null -w '%{http_code}\n' -L $url)"
done
-w '%{http_code}' writes a custom “what” string after the transfer — here the numeric status code — and -o /dev/null throws the body away. This one-liner checks which URLs in a list are alive, and is a classic scripting pattern.
Other useful write-out variables: %{time_total} (how long it took), %{size_download} (bytes received), %{remote_ip} (which server answered).
Common Pitfalls
- Forgetting
-L— the most commoncurlsurprise: the command “succeeds” but prints nothing because the server redirected and curl stopped at the redirect. - Confusing
-oand-O— lowercase names the file yourself; capital uses the server’s name. Mixing them up silently saves files under odd names. - Expecting a browser’s behavior —
curldoes not run JavaScript, does not accept cookies by default (use-c cookies.txt/-b cookies.txt), and sends noUser-Agentlike a browser does. Some sites block it for that reason. - Running
curl | bashfrom installers without reading it — piping a remote script straight into a shell runs whatever the server sent. Read the script first, or download it and inspect it.
Related Pages
| Page | Relationship |
|---|---|
| wget | The other downloader — better for whole sites and resumable transfers |
| nc | The raw-socket tool for when you need to speak the protocol yourself |
| ping | Checks whether the host is reachable before you ask it for data |
| traceroute | Shows the path when a host cannot be reached at all |
curl Cheatsheet
| Command | What it does |
|---|---|
curl example.com | Fetch a URL and print the body |
curl -I https://example.com | Show only the response headers |
curl -O URL | Download using the remote filename |
curl -o file URL | Download with your own filename |
curl -L URL | Follow redirects |
curl -v URL | Verbose request/response details |
curl -s URL | Silent mode (no progress bar) |
curl -X POST -d 'data' URL | Send a POST request with a body |
curl -H "Auth: Bearer TOKEN" URL | Send a custom header |
curl -F "[email protected]" URL | Upload a file as multipart form data |
curl -s -o /dev/null -w '%{http_code}' URL | Print just the HTTP status code |