cURL is used to send data or fetch data to/from a server. This data is transferred using the following protocols: FTP, HTTP, HTTPS, IMAP, POP3, SCP, SFTP, SMTP, TELNET, TFTP, and many more. cURL does not involve any user interaction. You can also use this tool when testing RESTful API and much more.
It also offers the following useful services:
- User Authentication
- FTP Upload
- HTTP Post
- Cookies
- SSL Connections
- and many more
Table of Contents
Install cURL on Linux
First, update your system by using the update command before installing it to any distribution of Linux.
For Debian-based Linux distributions like Ubuntu, Linux Mint, and Kali Linux:
apt-get install curl
Or
apt install curl
For Fedora-based distributions like CentOS and Red Hat Enterprise Linux (RHEL):
yum install curl
Install cURL on Mac
Run the following command to install it on Mac with Homebrew:
brew install curl
Basic cURL syntax
The syntax of the curl command/tool is as follows:
curl Options URLs
As you can see from the above syntax, you can use multiple options and multiple URLs.
You can run the following command to see the list of all the options you can use:
curl --help
How do you determine the version of cURL
Use the –V or –version option to check the version of the installed curl tool e.g.
curl -V
The command mentioned above also gives back the list of protocols you can use with this command and its features.
How to fetch/get data from a URL
By default, curl uses HTTP GET method.
The simplest command we can run is:
curl http://example.org/
As a result of running this command, you will see the HTML response and everything that a browser renders.
You can also store the response in a file using the -o or –output option:
curl -o data.html http://example.org/
If you know the response will be in the JSON form, then you can use the following command:
curl -o data.json http://example.org/
You can also run the above command without using the -o option:
curl http://example.org/ > data.json
Moreover, you can even download an image instead of HTML, JSON, or any other data e.g.
curl -o image.png https://www.iconfinder.com/icons/2993698/chrome_logo_logos_icon
You can also get the response header only by using the –I or –head option e.g.
curl -I http://example.org/
Moreover, if you want to get both the response header and the data, then use the -i or –include option:
curl -i http://example.org/
After the data is fetched from this URL, you will also get a response header at the top of this data.
Post data to a URL
You can post the data to a URL using -d or –data option:
curl -d "first=Anto&last=Online" URL
Specify the cURL HTTP method
You can also use your request method by using the –X or –request option:
curl -X PUT -d "first=Anto&last=Mian" URL
As you can see, we have used the request method as PUT, which means that we want to update the data on this URL. You can use PUT, DELETE, or any other request method with the -X or –request option.
Delete data using cURL
To delete the data lying on a specific URL path, you can use the -X option e.g.
curl -X DELETE URL
Authenticate using cURL
At times, you might need to authenticate a user before you send POST, GET, or any other request to a URL.
To authenticate the user, you can use the option -u or –user to specify the username and password. Remember, we put a colon after the username:
curl -u username:password URL
Store Cookies using cURL
To store the cookies for any website, you can use the -c or –cookie-jar option:
curl -c cookies.txt -O https://www.google.com/index.html
Note: You can use the “-“ instead of writing the fileName to write the cookies on the screen instead of writing them to a file:
curl -c - -O https://www.google.com/index.html
How to limit the download speed for cURL
You can limit the download speed for fetching any data from a URL by using the –limit-rate option:
curl --limit-rate 25K -o image.png https://www.iconfinder.com/icons/2993698/chrome_logo_logos_icon
Remember, the speed is measured in bytes per second. You can also specify a suffix like k or K for Kilobytes, m or M for Megabytes, g or G for Giga-bytes. The example above is set to transfer 25 Kilobytes per second.
To further learn about this tool, run the following command to go through all the manual pages:
man curl
You can also visit the official website for more info: https://curl.se/docs/manual.html.