6. Curl and the Language of HTTP
The Swiss army knife for talking to servers
curl transfers data using URLs. It speaks HTTP, HTTPS, FTP, SMTP, and
dozens of other protocols — but in practice, it's the tool you reach for
when you need to talk to a web server from the command line.
It was first released in 1998 by Daniel Stenberg and has been under active
development ever since. Today it ships with nearly every operating system
and runs on billions of devices. When documentation says "try this API
endpoint," the example is almost always a curl command.
The Simplest Request
curl https://example.com
This sends an HTTP GET request and prints the response body to stdout.
That's it — curl is just a program that makes HTTP requests and prints
what comes back.
Fetch a webpage
curl https://example.com
# <!doctype html>
# <html>
# <head>
# <title>Example Domain</title>
# ...
Seeing the Full Conversation: -v
The most useful flag for understanding HTTP is -v (verbose). It shows
the entire request and response, including headers:
curl -v https://example.com
Output (simplified):
> GET / HTTP/2
> Host: example.com
> User-Agent: curl/8.7.1
> Accept: */*
>
< HTTP/2 200
< content-type: text/html; charset=UTF-8
< content-length: 1256
<
<!doctype html>
...
Lines starting with > are what curl sent. Lines starting with < are what
the server returned. This is invaluable for debugging.
HTTP Methods
GET (default)
curl https://api.example.com/users
POST with -X or -d
Sending data with -d automatically switches to POST:
curl -d "name=alice&role=admin" https://api.example.com/users
The -d flag also sets Content-Type: application/x-www-form-urlencoded.
POST with JSON
For JSON APIs, set the content type explicitly:
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name": "alice", "role": "admin"}' \
https://api.example.com/users
Or use the --json shorthand (curl 7.82+):
curl --json '{"name": "alice"}' https://api.example.com/users
--json sets Content-Type: application/json, Accept: application/json,
and implies POST — three things you'd otherwise type by hand.
PUT, PATCH, DELETE
curl -X PUT -d '{"role":"user"}' -H "Content-Type: application/json" \
https://api.example.com/users/42
curl -X PATCH -d '{"role":"user"}' -H "Content-Type: application/json" \
https://api.example.com/users/42
curl -X DELETE https://api.example.com/users/42
Headers
Sending headers with -H
curl -H "Authorization: Bearer TOKEN123" \
-H "Accept: application/json" \
https://api.example.com/profile
Viewing response headers with -I or -i
# Headers only (HEAD request)
curl -I https://example.com
# Headers + body
curl -i https://example.com
Check if a resource exists
curl -I https://example.com/page
# HTTP/2 200 → exists
# HTTP/2 404 → not found
Saving Output
To a file with -o
curl -o page.html https://example.com
Using the remote filename with -O
curl -O https://example.com/files/data.csv
# saves as data.csv
Silent mode with -s
Suppress the progress bar (useful in scripts):
curl -s https://api.example.com/status | jq .
Add -S to still show errors even in silent mode:
curl -sS https://api.example.com/status
Following Redirects: -L
By default, curl stops at redirects and shows the 3xx response. Add -L
to follow the redirect chain:
curl -L https://example.com/short-link
See the redirect chain
curl -v -L https://example.com/redirect 2>&1 | grep "< HTTP"
# < HTTP/2 301
# < HTTP/2 302
# < HTTP/2 200
Authentication
Basic auth with -u
curl -u alice:password123 https://api.example.com/admin
This sends an Authorization: Basic header with base64-encoded credentials.
Bearer tokens
curl -H "Authorization: Bearer eyJhbG..." https://api.example.com/data
Cookies
# Save cookies to a file
curl -c cookies.txt -L https://example.com/login
# Send cookies from a file
curl -b cookies.txt https://example.com/dashboard
Uploading Files
Form upload with -F
curl -F "[email protected]" -F "description=My photo" \
https://api.example.com/upload
-F sends a multipart/form-data request — the same encoding browsers use
for file upload forms. The @ prefix means "read from this file."
Raw upload with --data-binary
curl -X PUT --data-binary @archive.tar.gz \
-H "Content-Type: application/gzip" \
https://storage.example.com/files/archive.tar.gz
Timeouts and Retries
# Connection timeout (seconds)
curl --connect-timeout 5 https://slow-server.example.com
# Total operation timeout
curl --max-time 30 https://slow-server.example.com
# Retry on transient failures
curl --retry 3 --retry-delay 2 https://flaky-api.example.com
Writing Output to Variables in Scripts
# Capture response and HTTP status code separately
response=$(curl -s -w "\n%{http_code}" https://api.example.com/status)
body=$(echo "$response" | head -n -1)
status=$(echo "$response" | tail -n 1)
if [ "$status" -eq 200 ]; then
echo "OK: $body"
else
echo "Failed with status $status"
fi
The -w (write-out) flag appends metadata about the transfer. Useful variables:
| Variable | Meaning |
|---|---|
%{http_code} |
HTTP status code |
%{time_total} |
Total time in seconds |
%{time_connect} |
Time to TCP connect |
%{time_starttransfer} |
Time to first byte |
%{size_download} |
Bytes downloaded |
%{url_effective} |
Final URL after redirects |
Measure response time
curl -s -o /dev/null -w "Connect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" \
https://example.com
Config Files and .curlrc
For repeated options, create ~/.curlrc:
# Always follow redirects
-L
# Always show errors in silent mode
-sS
# Default timeout
--max-time 30
Or use a per-request config:
curl -K my-request.conf
Practical Recipes
Test an API endpoint
curl -s https://api.example.com/health | jq .
Download with a progress bar
curl -# -O https://example.com/large-file.zip
POST a file as JSON body
curl --json @payload.json https://api.example.com/data
Check SSL certificate
curl -vI https://example.com 2>&1 | grep -A 6 "Server certificate"
Simulate a browser
curl -H "User-Agent: Mozilla/5.0 (X11; Linux x86_64)" \
-H "Accept: text/html" \
-L https://example.com
Rate-limit downloads
curl --limit-rate 1M -O https://example.com/big-file.iso
curl vs. wget
| Feature | curl | wget |
|---|---|---|
| Protocols | 25+ (HTTP, FTP, SMTP, ...) | HTTP, HTTPS, FTP |
| Output | stdout by default | file by default |
| Recursive download | No | Yes (-r) |
| Resume downloads | curl -C - |
wget -c |
| API testing | Excellent | Minimal |
| Scripting | Designed for it | More for downloading |
Use curl when you're talking to APIs or need fine control over requests.
Use wget when you're downloading files or mirroring sites.
Common Pitfalls
Forgetting -L
Many URLs redirect. Without -L, you get a 301/302 response body instead of
the actual content.
Quoting JSON in bash
Single quotes prevent bash from interpreting special characters in JSON:
# Good
curl -d '{"key": "value"}' ...
# Bad — bash expands $key
curl -d "{\"key\": \"$variable\"}" ...
# Better for variables — use a file or heredoc
curl --json @- https://api.example.com <<EOF
{"key": "$variable"}
EOF
HTTPS certificate errors
# Skip verification (development only — never in production)
curl -k https://self-signed.example.com
Summary
curlsends HTTP requests and prints responses — it's the command-line HTTP client-vshows the full request/response conversation (essential for debugging)-dsends POST data;--jsonis the shorthand for JSON APIs-Hsets custom headers;-uhandles basic authentication-Lfollows redirects;-osaves to a file;-ssilences progress output-wextracts response metadata (status codes, timing, sizes)- For API testing, debugging, and scripted HTTP,
curlis the universal tool