curl(short for Client URL) is a graphics-free, user-interface-less web browser that lives directly inside your terminal.
Just as you type an address into a browser (like Chrome or Firefox) to see a site, download an image, or fill out a form, curl does exactly the same things, but in a text-based way, at high speed, and with automation (scripting) capabilities. This tool supports many network protocols such as HTTP, HTTPS, FTP, SFTP, and more. It's an essential tool for every network professional, SysAdmin, and programmer.
Practical and User-Friendly Guide to Using curl
Below, we'll explore the most useful and interesting things you can do with this tool, complete with practical examples:
1. The Simplest Task: Fetching a Page's Content (GET Request)
If you want to see the HTML code or response from a website in your terminal, just type the tool name followed by the site's address:
curl https://example.com
2. Downloading and Saving Files
By default, curl prints the content in the terminal. To save it as a file, you have two options:
- Using the
-o(lowercase) switch: Saves the file with a name you choose.
curl -o my_page.html https://example.com
- Using the
-O(uppercase) switch: Downloads the file using its original name from the server.
curl -O https://example.com/images/logo.png
3. Resuming Interrupted Downloads
If you're downloading a large file and your connection drops, no need to start over! Use the -C - switch to continue from where it stopped:
curl -C - -O https://example.com/largefile.zip
4. Viewing HTTP Headers (Behind-the-Scenes Site Info)
Sometimes for troubleshooting or checking server status, you need to see the server's response headers (like status code 200 or 404, server type, cookies). Use the -I switch to fetch only the headers:
curl -I https://example.com
5. Following Redirects
Many sites redirect requests from HTTP to HTTPS or to a different address. By default, curl doesn't follow this path unless you use the -L switch:
curl -L http://google.com
6. Sending Data to the Server (POST Request)
If you're testing an API and want to send data to the server (e.g., filling out a form or submitting a payload), use the -X POST switch together with -d for the data:
curl -X POST -d "param1=value1¶m2=value2" https://example.com/api
Or if your data is in JSON format:
curl -X POST -H "Content-Type: application/json" -d '{"name": "Saleh", "role": "Admin"}' https://example.com/api/users
7. Using a Proxy and Tunneling
If your server is behind a firewall or you want to route traffic through a proxy (like HTTP or SOCKS5), curl makes this easy with the -x switch:
curl -x socks5h://127.0.0.1:1080 https://example.com
8. Authentication
If a site requires a username and password (Basic Authentication), you can send them with the -u switch:
curl -u username:password https://example.com/protected
Other Advanced and Interesting Features
- Limiting download speed: To avoid saturating your server's bandwidth:
curl --limit-rate 50k -O https://example.com/file.zip
- Changing the User-Agent: Introducing your terminal as a specific browser (e.g., Chrome on Windows) to bypass restrictions on some sites:
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://example.com
- Silent mode: Removing the progress bar and extra messages (very useful in scripts):
curl -s https://example.com
curl's Rivals
Generally speaking, the main rivals to curl are wget and httpie. Below, we examine each and compare their advantages:
1. wget (Old-school, tough-as-nails, downloading specialist)
If curl is like a sleek Swiss Army knife, wget is like a bulldozer. wget is a pure command-line tool for downloading files from the web.
wget's advantages over curl:
- Recursive Download:
wget's biggest trump card. You can give it one address and tell it, "download all the pages, images, and links from this entire site and make an offline copy."curlhas no such capability. - High resilience to connection drops:
wgetis extremely tenacious during long, unstable downloads. If the connection breaks, it will keep retrying until the file is fully downloaded. - Simplicity for direct downloads: For downloading a single file, you don't need extra switches – just type
wget urland the file is saved (withcurlyou must use the-Oswitch).
2. httpie (Modern, beautiful)
httpie (pronounced "HTTPie") is a modern, human-friendly alternative to curl, written in Python, designed to make working with APIs in the terminal easier.
httpie's advantages over curl:
- Colorful, readable output (Syntax highlighting): Unlike
curl, which prints everything as raw black-and-white text,httpieautomatically colorizes and pretty-prints JSON and HTML code, making it much easier to read. - Very simple syntax for APIs: To send a POST request with JSON in
curl, you need to write out headers and a complex structure. But withhttpie, it's much more natural:- Example in httpie:
http POST api.com name=Saleh
- Example in httpie:
- Automatic header detection: You don't need to tell it the data format – it figures out that you're sending a JSON request and sets the necessary headers automatically.
So, what is curl's advantage?
With such powerful rivals, why is curl still the world's number-one tool?
- The
libcurllibrary: The beating heart ofcurlis a library calledlibcurl. This library works behind the scenes in thousands of software applications, scripts, programming languages (like PHP and Python), and even operating systems (like Mac, Windows, and smart appliances) to transfer data. - Unmatched speed and lightness: Because it's written in C, it's extremely fast and uses minimal RAM and CPU (unlike
httpie, which, being Python-based, is a bit heavier). - Infinite flexibility: The number of switches and options for controlling traffic, cookies, proxies, and protocols in
curlis incomparable to any other tool.