Whether you‘re a software developer, sysadmin, or tech enthusiast, chances are you‘ve come across cURL at some point. Curious what this ubiquitous tool is all about? You‘ve come to the right place!
In this guide, we‘ll start from the very basics – understanding what is cURL, how it originated, what problem it solves and then go deeper into how you can use it effectively through hands-on examples.
By the end, you‘ll have a solid foundational knowledge of this powerful command-line tool to start curling like a pro!
A Brief History of cURL
Let‘s begin at the beginning.
cURL first came into existence in 1997, created by Daniel Stenberg to facilitate transferring data over various network protocols. Back then it was named "HTTP Get", focused solely on HTTP requests to fetch data.
Daniel was motivated by the need for a command-line tool that could make downloading data easy without complex code. This was particularly useful for developer communities at the time.
The first version supported FTP along with HTTP, and additional protocols were added subsequently. By 1998, the project was renamed to cURL to reflect its broader protocol support.
Over the past 25+ years, cURL has become ubiquitous – it comes pre-installed in virtually every operating system today! Quite an achievement.
Fun fact: The name cURL is not an acronym. The ‘c‘ is for client, while ‘URL‘ indicates it does internet transfers!
cURL vs Wget – What‘s the Difference?
If you‘ve dabbled in command-line transfers before, you may have heard of GNU Wget. It‘s a similar internet data transfer tool that predates cURL by a few years.
So what‘s the key difference between cURL and Wget?
While both can transfer data using HTTP and FTP, some major differences stand out:
cURL | Wget |
---|---|
Supports a wider array of protocols – SFTP, SCP, TFTP, TELNET, DICT, LDAP | Primarily designed for HTTP/HTTPS and FTP |
Used for APIs testing, automation, debugging | Focus on mirroring and downloading full sites |
Active development and support | No major updates recently |
In a nutshell:
- cURL is more universal for transfers while Wget excels at large scale mirroring of sites.
- cURL has an active community behind it while Wget development has stalled.
So cURL has wider protocol support and active development going for it!
What is cURL Used For?
Now that we‘ve understood the history and origins of cURL, let‘s look at why it‘s so popular among developers and system admins even today.
Some of the most common use cases of cURL include:
- Transferring data with protocols like HTTP, FTP, LDAP, MQTT, POP3, SMTP, Telnet etc.
- Making API calls to web APIs – cURL lets you easily test REST and SOAP APIs.
- Automation – cURL can be used to script repetitive tasks like data transfers.
- Web scraping – extract data from websites through scripts using cURL.
- Debugging – inspect request/response headers during development and testing.
- Downloading files on remote servers via SFTP, FTP etc.
- Uploading data and files to web servers and APIs.
cURL supports pretty much any use case that involves transferring data to or from a remote server. The list of supported protocols covers most common scenarios:
Protocol | Usage |
---|---|
HTTP/HTTPS | Access websites and APIs |
FTP / SFTP | Transfer files |
IMAP / POP3 / SMTP | Email related protocols |
MQTT | Connect to IoT and messaging devices |
LDAP | QueryLDAP directories |
TFTP | Transfer files over TCP/UDP |
Thanks to its ubiquity, you can rely on cURL being available on any modern operating system or platform you‘re using.
Next, let‘s get our hands dirty and see cURL in action!
Installing cURL
Chances are cURL is already installed on your system. You can check by running:
curl --version
This will print the installed version of cURL if available.
If not installed, use your operating system‘s package manager to install cURL:
- Linux: Install using the native package manager –
apt
,yum
,zypper
, etc. - Windows: Download binaries from the cURL website or use a package manager like Chocolatey.
- MacOS: Use Homebrew –
brew install curl
That‘s it – you‘re ready to start using cURL!
How to Use cURL on the Command Line
cURL uses a simple command line syntax to transfer data from or to a server.
The basic format is:
curl [options] [URL]
Let‘s look at some common examples of using cURL.
1. Send a GET Request
To send a GET request, pass the URL after curl
:
curl https://example.com
This will make a request to the URL and print the response body to stdout.
You can also save the response in a file instead of printing to stdout using the -o
flag:
curl -o response.txt https://example.com
2. Send a POST Request
To send a POST request with data, use the -d
or --data
option:
curl -d ‘name=John&age=25‘ https://example.com/form
This will POST the data name=John&age=25
to the URL.
You can also specify the data from a file using -d @file
.
3. Set Request Headers
To set a custom header, use -H "Header: Value"
:
curl -H "Content-Type: application/json" https://example.com
You can set multiple headers by passing -H
multiple times.
4. Follow Redirects
By default, cURL does not follow redirects. To tell cURL to follow redirects, use -L
:
curl -L https://example.com
Now cURL will follow any 301, 302, or other redirect status codes.
5. Download a File
To download a file from a URL, use -o
to specify the output file name:
curl -o example.zip https://example.com/example.zip
This will download the file and save it as example.zip
.
6. Limit Rate
You can limit the transfer speed using --limit-rate
:
curl --limit-rate 200k https://example.com
This will limit the download speed to 200 KB/s.
7. Fetch Headers Only
To get only the header from a request without downloading the body, use -I
or --head
:
curl -I https://example.com
8. Debug Requests
To see the full request and response headers, use -v
:
curl -v https://example.com
This prints the request and response header details so you can debug connections.
…(examples truncated for brevity)
This covers some of the most common options for using cURL. The full list has over 200 options – refer to curl --help
for details on all available options.
Now let‘s go over how to use cURL for some real-world use cases.
Using cURL for Common Real-World Tasks
While cURL is most commonly used by developers to test APIs, it can simplify many repetitive tasks.
Here are some examples of using cURL for common scenarios:
Automate Data Transfer Tasks
For recurring data transfer needs like backups, you can automate via cron jobs using cURL instead of a custom script.
For example, backing up files to an SFTP server daily:
# cron entry to run at 1 AM daily
0 1 * * * curl -u user:pass -T /path/to/files sftp://backup.example.com/
Test REST API Endpoints
cURL lets you quickly test REST APIs without needing to write code.
To POST JSON data to an API:
curl -X POST -H "Content-Type: application/json" -d ‘{"name":"John"}‘ https://api.example.com/users
To access an authenticated API, pass the access token:
curl -H "Authorization: Bearer <token>" https://api.example.com/users
This saves you from writing testing code for early API iterations.
Download Files from Remote Servers
Want to quickly download a file from an FTP or SFTP server? cURL lets you easily do this:
# Download via FTP
curl -u user:pass -o myfile.zip ftp://example.com/files/myfile.zip
# Via SFTP
curl -u user:pass sftp://example.com/path/to/file
No need to use a separate FTP/SFTP client for simple transfers.
Submit Web Forms
Need to automate form submissions for testing? cURL allows POSTing form data:
curl -d ‘[email protected]‘ https://example.com/subscribe
This submits the form with the given data.
Fetch Website Data
Combining cURL with a parsing tool like grep lets you extract information from websites:
curl https://example.com | grep -Po ‘(?<=<title>)[^<]+‘
This returns the contents within the <title>
tag of a website.
As you can see, cURL can simplify many common tasks involving data transfers and web APIs. The use cases are virtually endless!
Why Use cURL Over Developer APIs and Libraries?
While you can achieve most cURL use cases by writing code using libraries like Python Requests, here are some benefits of using cURL directly:
- No dependencies – cURL works over a simple command line interface and has no external dependencies. APIs require managing dependencies and language versions.
- Ubiquity – Chances are cURL is already installed on most systems you will encounter. APIs require setting up the execution environment.
- Ease of use – Simple commands vs writing code. The learning curve is lower with cURL to do simple tasks.
- Portability – cURL commands work across any operating system. Code using libraries needs to be portable across platforms.
- Interactive – Easy to test requests and debug interactively via CLI. Coding and testing involves edit-compile-run cycles.
So while developer libraries provide more structure, cURL should be your go-to for quick tasks, debugging, and automation!
Advance cURL Usage Tips and Tricks
Let‘s round up this guide with some pro tips:
-
Use cURL‘s output redirect
-o
to download files or save response body to disk. -
Pass
-I
or--head
to make HEAD requests and view headers only. -
Use
--trace-ascii
to log requests to a debug file for diagnosing issues. - Chron and bash can help schedule and automate cURL.
-
Set the
-w
flag to customize output and only show what you need. -
Compress requests using
--compress
to save bandwidth.
That concludes my in-depth guide on understanding cURL and leveraging it for your daily data transfer needs! Here are some key takeaways:
- cURL is a versatile command line tool for transferring data using various network protocols.
- It can be used for automation, testing APIs, debugging, downloading files, submitting forms and more.
- cURL has an extensive set of options to tweak requests as per your needs.
- Prefer cURL over developer libraries for quick tasks, portability and ease of use.
- It comes pre-installed on most modern operating systems and platforms.
I hope this guide gave you a firm grounding in applying cURL effectively. Happy curling!