The most useful curl commands are the ones you use daily: fetching a URL with curl https://example.com, saving output with -o, following redirects with -L, and sending POST data with -d. This article covers those and a dozen more practical variations, each with a concrete example you can copy and adapt.
What curl Actually Does
curl (short for “client URL”) is a command-line tool that transfers data to or from a server using protocols like HTTP, HTTPS, FTP, SFTP, and many others. It is installed by default on most Linux distributions, macOS, and Windows 10+ (as curl.exe). The tool sends a request exactly as you specify, then prints the server’s response to the terminal unless you tell it otherwise. That simple model makes it endlessly useful for testing APIs, downloading files, checking headers, and debugging network issues.
curl is not a browser. It does not render HTML, execute JavaScript, or follow redirects unless you ask it to. This is a feature: you see exactly what the server returns, which is why developers and system administrators rely on it.
Basic Fetch and Save
The most basic command is curl https://example.com. It fetches the page and prints the HTML to stdout (your terminal). To save the output to a file, use -o with a filename:
curl -o page.html https://example.com– saves the body to page.html.curl -O https://example.com/file.zip– saves the file with its remote name (note the capital O).curl -o /dev/null -s https://example.com– discards output, useful for timing or testing.
If you want to see the response headers along with the body, add -i (include) or -I (head only). The latter sends a HEAD request, which returns headers without the body – perfect for checking if a URL exists or what content type it serves.
Following Redirects and Handling Errors
Many URLs redirect (HTTP 301 or 302). By default, curl does not follow them. Add -L to follow redirects until the final destination. For example, curl -L https://example.com will follow any redirects and print the final page. Without -L, you get the redirect response (usually a 3xx status) and nothing else.
To see the HTTP status code of a request, use -w with a format string:
curl -s -o /dev/null -w "%{http_code}\n" https://example.com
This prints just the status code, like 200 or 404. You can also extract the time taken, size, and other metrics with %{time_total}, %{size_download}, etc.
Sending Data: POST, PUT, and JSON
APIs often require sending data. The -d flag sends data in a POST request with the content type application/x-www-form-urlencoded. For example:
curl -d "name=John&age=30" https://example.com/api
To send JSON, use -H to set the content type and -d with a JSON string:
curl -H "Content-Type: application/json" -d '{"name":"John","age":30}' https://example.com/api
For PUT or DELETE, use -X to specify the method:
curl -X PUT -d "name=Jane" https://example.com/api/user/1
Note that -X changes the method but does not affect how -d works. For file uploads, use -F (form data): curl -F "file=@local.txt" https://example.com/upload.
Authentication and Headers
Many endpoints require authentication. curl supports basic auth with -u username:password. For bearer tokens (common in OAuth), add an Authorization header:
curl -H "Authorization: Bearer TOKEN" https://api.example.com/data
To send a custom header, like a user agent or referrer, use -H again. You can also set a cookie with -b (cookie string) and store cookies with -c (cookie jar file). For example, to log in and then fetch a page:
curl -c cookies.txt -d "user=me&pass=secret" https://example.com/login
curl -b cookies.txt https://example.com/account
Timeouts, Retries, and Proxies
When scripting, you want to avoid hanging forever. Use --max-time to limit total time (e.g., --max-time 10 for 10 seconds) and --connect-timeout for just the connection phase. To retry failed requests, use --retry N (e.g., --retry 3). For proxies, set -x http://proxy:port or use environment variables like HTTP_PROXY.
If you need to ignore SSL certificate errors (for testing only, never in production), use -k. But be aware this makes the connection insecure.
Common Pitfalls and Misconceptions
A frequent mistake is forgetting to quote URLs that contain & or ?. The shell interprets these characters, so always wrap the URL in quotes: curl "https://example.com/?a=1&b=2". Another is using -X POST without -d – that sends an empty POST body, which may not be what the server expects. Also, -d automatically sends a POST, so adding -X POST is redundant unless you need to override a method.
People also confuse -o (output file) with -O (remote filename). A single typo can overwrite your own file or save to the wrong name. Finally, remember that curl does not follow redirects by default – if you get a 301/302 response, you need -L.
Frequently Asked Questions
How do I see only the response headers?
Use curl -I https://example.com. This sends a HEAD request and prints only the headers. If the server does not support HEAD, use curl -i to include headers with the body, or curl -D - -o /dev/null to dump headers to stdout and discard the body.
How do I download a file with curl and keep the original name?
Use curl -O https://example.com/file.zip. The capital O tells curl to use the remote filename. If you want a different name, use lowercase -o with your own filename.
What does the -s flag do?
-s stands for silent mode. It suppresses the progress meter and error messages. Combined with -o /dev/null, it makes curl completely quiet, which is useful in scripts where you only care about the exit code or the output from -w.
Can curl send a GET request with parameters?
Yes, just put the parameters in the URL: curl "https://example.com/search?q=curl". If you use -d, curl sends a POST instead. To force a GET with query parameters, use -G along with -d: curl -G -d "q=curl" https://example.com/search.
How do I test if a URL is reachable from the command line?
Run curl -s -o /dev/null -w "%{http_code}" https://example.com. If it prints 200, the URL is reachable. A 404 means the server responded but the resource is missing. If curl fails to connect, you will see an error message (unless you used -s).
Is curl safe to use with sensitive data like passwords?
It depends on the protocol. With HTTPS, the connection is encrypted, so data in transit is protected. However, the command line itself may be visible in your shell history or process list. For production, use config files or environment variables for secrets, and never put credentials directly in a command you paste publicly.
Related
Formula used
in url | URL | - | https://example.com | 0 | 100000 in data | POST data | - | name=John&age=30 | 0 | 100000 out len | Response length | bytes | 0 | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Motorola Phone Recovery: How to Recover Data
- Poco X3 GT Dimensity 1100 67W Fast Charging Launched?5.00 min/km
- Flash Converter: What It Is & How to Use It
- Choosing a Pipe Flow Meter for Swirling Flow
- Convert 2D Videos to Oculus Rift VR: How-To
- DRAM Light on Motherboard: Meaning & Fixes5.00 min/km