Curl 20 Essential Tips for Beginners
In this article
This article explains the 20 most useful curl commands and concepts for beginners, covering what each command does, when to use it, and common pitfalls. You will learn how to transfer data, download files, test APIs, and handle authentication, all from the command line.
Why curl is worth learning
curl (client URL) is a command-line tool present on almost every Unix-like system, including Linux and macOS, and available for Windows. It lets you transfer data using protocols like HTTP, HTTPS, FTP, and more. For developers, system administrators, and anyone who works with APIs, curl is often the quickest way to test an endpoint, download a file, or debug a server response.
Beginners sometimes think curl is only for downloading files, but it is far more powerful. You can send POST requests, set custom headers, handle cookies, upload files, and inspect response headers. The 20 tips below cover the most common tasks you will face.
Essential curl commands and options
Here are the core options you will use daily. Each one is a tip in itself.
- 1. Basic GET request:
curl https://example.comfetches the page and prints the body to stdout. - 2. Save output to a file: Use
-o filenameto save the body, or-Oto save with the remote filename. Example:curl -O https://example.com/file.zip. - 3. Show response headers:
-iincludes headers in the output;-Isends a HEAD request and shows only headers. - 4. Follow redirects:
-Ltells curl to follow HTTP 3xx redirects. Without it, you only get the redirect response. - 5. Silent mode:
-ssuppresses the progress meter and error messages. Combine with-Sto show errors but not the progress bar. - 6. Verbose output:
-vprints detailed information about the request and response, including TLS handshake and headers. Use--tracefor even more detail. - 7. Set a timeout:
--max-time 30limits the total operation to 30 seconds.--connect-timeout 10limits only the connection phase. - 8. Send a POST request:
-d "key=value"sends data as the request body, defaulting toapplication/x-www-form-urlencoded. - 9. Send JSON data: Use
-H "Content-Type: application/json" -d '{"name":"test"}'. - 10. Custom headers:
-H "Authorization: Bearer TOKEN"adds any header you need. - 11. Basic authentication:
-u username:passwordsends HTTP Basic Auth. For API tokens, use the Authorization header instead. - 12. Upload a file:
-F "file=@localfile"sends a multipart/form-data request, typical for file uploads. - 13. Download with resume:
-C -continues a partial download. Useful for large files. - 14. Limit download speed:
--limit-rate 200Kcaps the transfer rate to 200 kilobytes per second. - 15. Use a proxy:
-x http://proxy:portroutes the request through a proxy. - 16. Ignore SSL certificate errors:
-kbypasses certificate verification. Only for testing—never in production. - 17. Specify a certificate:
--cacert ca.pemuses a custom CA bundle for verification. - 18. Cookies:
-b cookies.txtsends cookies from a file;-c cookies.txtsaves received cookies to a file. - 19. Retry failed requests:
--retry 3retries the request up to 3 times on transient errors. - 20. Output to stdout vs file: Use
curl ... | jqto pipe JSON into a parser, or-o fileto save. Remember that-ois for the body, not the headers.
How to read curl output and debug
When a request fails, curl gives you clues. The exit code 0 means success; 6 means couldn't resolve host; 7 means connection refused; 28 means timeout. Use echo $? after a command to see the exit code. The -v flag shows the exact request line, headers, and TLS details. If you see SSL certificate problem, you may need -k (testing only) or the correct CA bundle.
Another common issue is the progress meter cluttering output. Use -s to silence it, but remember that -s also hides errors—so use -sS to show errors. When piping to another program, always use -s to avoid corrupting the data with the meter.
Common mistakes and how to avoid them
- Forgetting quotes around data:
-d '{"key":"value"}'needs single quotes to prevent the shell from interpreting braces and spaces. - Using
-Owhen you want a custom filename:-Ouses the remote name; for local control use-o myfile. - Not following redirects: Many URLs redirect (e.g., http to https). Without
-L, you get an empty body. - Confusing
-dwith-F:-dsends URL-encoded data;-Fsends multipart/form-data. APIs expect one or the other—check the docs. - Leaving passwords in history: Use
-u user:passcarefully; the command appears in shell history. Prefer environment variables or--netrc.
curl vs. wget vs. Postman
wget is a simpler downloader, good for mirroring websites, but it lacks the fine-grained control of curl for APIs. Postman provides a graphical interface for building requests, which is great for exploration, but curl is scriptable and available everywhere. For automated tests and CI/CD, curl is the standard. You can export a request from Postman as a curl command, which is a handy way to learn.
Security and safety practices
Never use -k in production—it disables certificate validation and exposes you to man-in-the-middle attacks. When sending sensitive data, ensure the URL uses HTTPS and that you are not leaking credentials in the command line. Use --netrc to read credentials from a protected file, or rely on environment variables. Also be careful with -v in logs: it can print Authorization headers, so avoid it in scripts that log output.
Frequently Asked Questions
How do I see only the HTTP status code?
Use curl -o /dev/null -s -w "%{http_code}" https://example.com. The -w option writes a custom output, and %{http_code} gives just the status.
Why does curl return an empty response?
Often because the server sent a redirect and you didn't use -L. Check with -I to see the headers and look for a Location field.
How do I send a PUT or DELETE request?
Use -X PUT or -X DELETE. For example: curl -X PUT -d "data" https://api.example.com/item/1. Some APIs also accept POST with an override header.
What is the difference between -d and --data-binary?--data-binary sends the data exactly as given, without stripping newlines or carriage returns. -d strips newlines, which can break JSON payloads. Use --data-binary for raw JSON.
How do I download a file with a specific name?
Use -o filename to set the local filename. For example: curl -o myfile.zip https://example.com/download.
Can I use curl with a proxy that requires authentication?
Yes, use -x http://user:pass@proxy:port or set the http_proxy environment variable. For HTTPS proxies, use --proxy with the full URL.