The curl command is a powerful tool for transferring data to and from remote servers. With curl, you can download files, interact with APIs, send data to a server, automate tasks, and much more. In this comprehensive guide, we will explore the intricacies of curl to help you master this essential command.
An Introduction to Curl
Curl stands for "Client URL". It is a command-line tool that allows you to make HTTP/HTTPS/FTP requests and transfer data without user interaction. Some key features of curl include:
- Supports numerous protocols – HTTP, HTTPS, FTP, FTPS, SMTP, POP3, IMAP, LDAP, MQTT, etc.
- Can upload/download files and print response headers
- Offers hundreds of options to customize requests
- Works on Linux, macOS, Windows and UNIX systems
- Free and open-source
In a nutshell, curl acts as a client and communicates with remote servers. You can use it to automate tasks like calling APIs, downloading web pages, transferring files, sending emails, and more.
Here is the basic syntax of curl:
curl [options] [URL...]
To download a web page:
curl https://www.example.com
Now let‘s explore some common uses cases of curl.
Downloading Files with Curl
One of the most popular uses of curl is to download files from remote servers. The curl command saves us the effort of opening a web browser and clicking on links to trigger downloads.
Here is how to download a file using curl:
curl -O https://upload.wikimedia.org/wikipedia/en/b/b0/Rick_Astley_-_Never_Gonna_Give_You_Up.png
This will download the image file to your current working directory. The -O
flag tells curl to use the remote file‘s name for saving instead of a random name.
You can also provide a custom local file name to save the download:
curl -o rickroll.png https://upload.wikimedia.org/wikipedia/en/b/b0/Rick_Astley_-_Never_Gonna_Give_You_Up.png
To resume an interrupted download, use the -C -
option:
curl -C - -O https://upload.wikimedia.org/wikipedia/en/b/b0/Rick_Astley_-_Never_Gonna_Give_You_Up.png
This will continue the download from where it left off. The -C -
basically tells curl to find the correct offset bytes to restart the transfer.
Using Curl for APIs
Curl is commonly used to interact with the APIs of web services. For example, let‘s access a random joke API:
curl https://icanhazdadjoke.com/
This returns a random dad joke in JSON format:
{"id":"R7UfaahVfFd","joke":"Why can‘t bicycles stand up on their own? They are two tired."}
We can pass the -i
flag to view response headers along with the content:
curl -i https://icanhazdadjoke.com/
HTTP/2 200
server: nginx
content-type: application/json;charset=UTF-8
etag: W/"65-IncH6Olhalwr/SiZU9oiPrOEdc"
date: Tue, 21 Feb 2023 16:16:52 GMT
content-length: 101
{"id":"R7UfaahVfFd","joke":"Why can‘t bicycles stand up on their own? They are two tired."}
To send data to APIs, you need to use HTTP methods like POST and provide data. For example, let‘s POST data to a dummy API:
curl -X POST -H "Content-Type: application/json" -d ‘{"name":"John"}‘ https://reqbin.com/echo/post/json
This sends a JSON payload and returns the response:
{
"success": true,
"id": "dibsy",
"json": "{\"name\":\"John\"}"
}
As you can see, curl enables you to seamlessly integrate with web APIs without the need for coding!
Transferring Data with Curl Post
The curl command can transfer data directly to a server using various protocols. This allows automation of backups, uploads, webhooks and more.
To demonstrate, let‘s create a simple HTML page and transfer it to a dummy upload server using curl‘s POST method:
<!-- file.html -->
<html>
<head>
<title>Uploaded by cURL</title>
</head>
<body>
<p>This page was transferred using cURL.</p>
</body>
</html>
Now we will send this page to the server:
curl -F "file=@file.html" https://posttestserver.com/post.php
The -F
option lets you specify data to POST in the key=value
format. We are reading the contents of file.html
and sending it under the file
parameter.
This uploads and returns a response from the server:
https://posttestserver.com/data/2022/06/13/1654984674768957.html
And we can access the uploaded file from this link in the browser!
Similarly, you can automate file transfers and backups to remote servers using curl‘s upload capabilities.
Automating Requests with Curl Scripts
While curl commands can be useful for ad-hoc transfers, you can also create executable scripts for automating tasks.
For example, here is a simple script that fetches and prints the current price of Bitcoin using an API:
#!/bin/bash
# Fetch latest Bitcoin price
btc_price=$(curl -s https://api.coindesk.com/v1/bpi/currentprice.json | jq -r ‘.bpi.USD.rate‘)
# Print output
echo "1 BTC is currently worth $ $btc_price USD"
We use jq
to parse the JSON response and extract the Bitcoin price. This script can be scheduled to run every few minutes and send you alerts.
The possibilities are endless – you can create scripts to back up data, monitor web apps, automate reports, and much more. Curl handles all the request and transfer work so you can focus on the business logic.
Common Curl Options
Now that you have seen basic examples, let‘s go through some commonly used curl flags and options:
Option | Description |
---|---|
-o |
Saves output to file instead of stdout |
-O |
Saves output to file named like the remote file |
-C - |
Resume incomplete download |
-v |
Enables verbose logging with detailed output |
-I |
Only fetch response headers |
-u |
Send username and password for server authentication |
-X |
Specify HTTP method (GET, POST, PUT etc.) |
-H |
Send custom header(s) with the request |
-d |
Send POST data in the request body |
--limit-rate |
Limit data transfer speed |
-4 -6 |
Force IPv4 or IPv6 address |
-L |
Follow HTTP redirects |
Consult the curl man pages to learn about advanced capabilities like proxies, cookies, FTP transfers and more.
Common Curl Pitfalls
While curl is generally simple to use, here are some mistakes that developers often make:
- Forgetting to quote URLs that have
&
,?
or other special characters - Not URL-encoding the payload data correctly using
-d
or-F
- Not handling authentication, SSL verification or HTTP redirects properly
- Using insecure options like
-k
without understanding risks - Parsing JSON blindly without proper error handling and checks
Be vigilant of these issues to avoid frustrations! Refer to documentation and StackOverflow if something does not work as expected.
Fun With Curl
Now that you have a good grasp of curl‘s capabilities, what can you do for fun with it? Here are some neat ideas:
- Get your public IP –
curl icanhazip.com
- Test internet speed –
curl speed.cloudflare.com/download
- Lookup DNS records –
curl https://dns.google.com/resolve?name=example.com
- Get stock quotes –
curl https://query2.finance.yahoo.com/v7/finance/quote?symbols=AMZN
- Translate text –
curl https://translate.googleapis.com/translate_a/single?client=gtx&sl=de&tl=en&dt=t&q=Hallo+Welt
- Validate email address –
curl -s --data-urlencode address=<email> https://mailboxvalidator.com/v1/validation
- Weather information –
curl wttr.in
Sky is the limit when it comes to creating mini command line apps powered by public APIs and curl!
Conclusion
The curl command line tool is versatile, flexible and extremely handy for transferring data, automating workflows and interacting with web services. With support for multiple protocols, hundreds of options and third-party integrations, curl can handle pretty much any data transfer job.
It may take some practice getting used to curl‘s capabilities and command line usage. But once mastered, curl massively boosts your productivity by letting you focus on core programming logic rather than network plumbing. Refer to this guide and official docs whenever in doubt.
So open up your terminal, fire up curl and start getting those bits and bytes flowing!