curl is a powerful command-line utility to communicate with a web server via the terminal. Often, it gets confused with the popular wget command as both of them can upload and download contents to a server and they do work cross-platform. So, to clear things up, here are the differences between wget and curl and some popular curl commands that you should know to start off.
curl vs wget
On the surface, both curl and wget can be seen as a utility that can request and download content from the web servers. However, if you dig down deep, there is a lot to unravel. curl supports many more protocols ranging from HTTPS, SFTP to IMAP, POP3, etc whereas, wget only supports HTTP, HTTPS, and FTP.
Another major difference is that wget supports recursive download whereas curl doesn’t. So, wget will download everything from the webpage to pages it is linked with whereas curl won’t. On the other hand, curl is based on the libcurl library which provides APIs. Hence, it can be easily used in command-line scripting as well as GUI based apps. You can think of curl as a stripped-down command-line web browser which can upload and download contents from a web server.
Installing curl on Windows
curl comes pre-installed on Unix-based or Unix-like Operating systems and hence it can be directly used on macOS. However, it can also be used on non-Unix systems like Windows with the help of a “libcurl” library.
In Windows, all we have to do is download the libcurl zip file and make a couple of changes to make it usable via the command prompt. Firstly, download the libcurl zip file for your Windows 32-bit or 64-bit accordingly. After you have downloaded the file, extract the contents in a folder. For context, the following is a screenshot of the extracted contents of the libcurl folder.
Within the extracted folder, head over to the bin folder and copy the following files – “curl.exe” and “curl-ca-bundle”.
Create a folder called “curl” within the “C:” drive and paste both the copied files. Once you have done this, you can directly use the curl command when you navigate to the “C:\curl” folder within the command prompt.
Alternatively, if you would like to run curl directly from the cmd, I would recommend creating an environment variable. To do that, head over to the Start menu and type “Edit the system environment variables” and click on the first search.
On the System Properties pop-up window, click on the Environment variables button at the bottom.
On the environment variable screen, click on the New button on the upper half to add a user environment variable.
In the new User Variable screen, add the following entry. Post that, click on Ok and next Apply to save the changes.
Variable name: curl Variable value: C:\curl
Once done, click on Ok under the Environment variable menu to save the changes. Post this, you should be able to use the curl command directly on the command prompt. To test it, enter the following command
Alternatively, you can also enable WSL for Windows and use the Ubuntu bash shell to run curl commands from bash on Windows.
Best Curl Commands and How to Use It
Before we begin with the commands, do note that single quotes don’t work in the Windows command line. You would have to convert them to double-quotes. For the bash shell, it would work as usual with the single quotes as well.
1. Browse HTML code
The most basic utility of curl is to browse the HTML code of a webpage. To do that, suffix the curl command with the web URL of the webpage.
This command will display the output on the terminal. You can also download the contents of the file by specifying the “-O” switch prior to the URL. This will save the file on your system with the same remote file name. For instance, if the HTML file name on the server is “home.html”, it will be saved locally as “home.html” as well.
curl -O http://example.com/home.html
Alternatively, you can store the data with custom file names. For that, append the command with a smaller case “-o” followed by the file name.
curl http://example.com/home.html -o sample-file.txt
You can also download multiple files by specifying multiple remote file URLs.
2. Downloading files with curl
Similar to wget, you can download files using curl. You can use the -o switch and specify the file name or else the file will be saved with the remote file name.
curl -O http://example.com/linux.iso
For example, the following command will download the latest version of Linux mint from the webserver.
curl -O http://mirrors.gigenet.com/linuxmint/iso/stable/19.2/linuxmint-19.2-cinnamon-64bit.iso
You can also use “-#” or “–progress-bar” to display the progress in the form of hashes.
3. Resuming Failed Download
Moreover, if the download is interrupted, you can resume by using the “-C -” switch. The command will look like as follows.
curl -C http://example.com/linux.iso
For example, the following command will resume the download from the last failed point.
curl -C - -O http://mirrors.gigenet.com/linuxmint/iso/stable/19.2/linuxmint-19.2-cinnamon-64bit.iso
4. IP Information
You can use the following curl command to get all the network information about a machine
Alternatively, if you know the IP address and you want other details like the location co-ordinates, city, timezone, etc, you can use the following variation of the command.
5. Send Emails
Since curl supports SMTP, POP3 protocol, you can even use it to send emails. Below is a sample of sending an email via Gmail.
Replace the email address and password in the following command accordingly.
curl --url "smtps://smtp.gmail.com:587" --ssl-reqd --mail-from "email@example.com" --mail-rcpt "firstname.lastname@example.org" --upload-file "C:\mail.txt" --user "email@example.com:password"
Make sure you turn on access for less secure apps for the Google account
The dict protocol provided by the libcurl can be used to easily find the meaning of a word. The source of the meaning is WordNet. Below is the command to do the same.
7. POST commands
You can send POST requests to a web server with the help of a curl command. The syntax is as follows
curl -X POST -F "name=user-name" -F "password=password" http://www.example.com
The POST form data can also be sent in the form of a JSON file. Use the following command to do that.
curl -H "Content-Type: application/json" https://www.example.com
8. Convert Files
Since curl supports multipart form data, you can use it to upload files and even get them converted instantly. For instance, I want to get this HTML file converted to a PDF. Hence, I will be using the Docverter API to do this. The Docverter API supports multiple file-formats from HTML, Markdown and can convert them to Docx, PDF, ePub, etc.
Alternatively, you can also use the Google Drive API v3 to convert excel spreadsheets, documents, etc.
curl "http://c.docverter.com/convert" -F from=html -F to=pdf -F input_files=@your-file-name -o "output-file-name.pdf"
curl is majorly used in programs for the sharing of data, files, forms. Apart from the above-mentioned command, there are various other APIs, combinations, or shell scripts you can use curl with. You can know more about this on the GitHub repos or Stack Overflow. For more issues or queries regarding any curl command, let me know in the comments below.
Also Read: 14 Best Learn to Code Apps (Android & iOS)