Dec 01, 2023
What Is a cURL Command and How to Use It
The cURL command is a useful Linux tool for data transfer and connection troubleshooting. On the client side, cURL is powered by libcurl, a free URL transfer library.
Let’s go deeper and figure out how to use it.
What Is cURL Command?
cURL (Client URL) is a command-line tool that allows data transfer to or from a server without user interaction using the supported libcurl library. cURL can also be used to troubleshoot connection issues.
cURL Command Syntax and Options
This is the basic syntax of the cURL command:
curl [OPTIONS] [URL]
Without any option, cURL will display the URL’s content in HTML format. For example, entering the following will show the content of hostinger.com‘s homepage:
You can customize the cURL command further by incorporating these options:
- -X <HTTP_METHOD> – specifies which HTTP method to use.
- -H “HeaderName: HeaderValue” – sets custom HTTP headers for the request.
- -d “data” – sends data in the request body.
- -o <output_file> – saves the response to a file.
- -O – saves a remote file.
- -i – includes HTTP response headers in the output.
- -L – follows HTTP redirects.
- -k – allows connections to SSL sites without certificates.
- -v – provides verbose output for debugging.
For instance, the following command lets you download file.txt from example.com:
curl -O https://example.com/file.txt
How to Check cURL Version
First, let’s check what version of cURL is available with the following command:
The output will show the cURL version and a list of supported protocols.
How to Use cURL Command in Linux
Now that you understand the fundamentals of the cURL command, let’s go deeper and look at some practical uses.
How to Use cURL Command for HTTP
cURL can be used for a proxy server. If you are behind a proxy server listening on port 8090 at sampleproxy.com, download the files as shown below:
curl -x sampleproxy.com:8090 -U username:password -O http:// testdomain.com/testfile.tar.gz
In the above example, you can skip -U username:password if the proxy does not require an authentication method.
A typical HTTP request will always contain a header. The HTTP header sends additional information about the remote web server along with the actual request. Using a browser’s developer tools, you can check the header information and verify it using a cURL command.
Below is an example of how to retrieve header information from a website.
curl -I www.testdomain.com
Using cURL, you can make a GET and a POST request. A GET request will be as follows:
A sample of a POST request will be as shown below:
curl –data “text=Hello” https://myDomain.com/firstPage.jsp
Over here text=Hello is the POST request parameter. This behavior would be similar to HTML forms.
You can also specify multiple HTTP methods in a single cURL command. Do this by using –next option:
curl –data “text=Hello” https://myDomain.com/firstPage.jsp --next https://myDomain.com/displayResult.jsp
This contains a POST request followed by a GET request.
Every HTTP request will have a user agent. This indicates the web browser details of the client. By default, a cURL request contains curl and the version number detailed by the user agent. Here’s a sample of the output:
“GET / HTTP/1.1” 200 “_” ”curl/7/29/0”
You can change this default user agent information using the below command:
curl -I http://mydomain.com –-user-agent “My new Browser”
Now, the changed output will be:
“GET / HTTP/1.1” 200 “_” ”My new Browser”
How to Use cURL for Cookies
cURL commands can be used to check what cookies get downloaded on a URL. So, if you are accessing https://www.samplewebsite.com, you can output the file, save the cookies, and access them using the Cat or VIM editor.
Here’s a sample command for this use case:
curl --cookie-jar Mycookies.txt https://www.samplewebsite.com /index.html -O
Similarly, if you have the cookies in a file, you can send it to the website. An example of such a command is shown below:
curl --cookie Mycookies.txt https://www. samplewebsite.com
How to Use cURL for FTP
cURL command supports FTP, so you can use it to download files from a remote server.
curl -u username:password -O ftp://sampleftpserver/testfile.tar.gz
In the above command, ftp://sampleftpserver is an FTP server that accepts connections. The username and password can be skipped for anonymous FTP connections. Enter the command and watch the progress bar fill up.
You can upload files as well with the command below:
curl -u username:password -T testfile.tar.gz ftp://sampleftpserver
How to Limit cURL Output
While using cURL, you can restrict the bandwidth to make sure it’s not throttled.
The below command restricts the bandwidth to 100K:
curl --limit-rate 100K http://testdomain.com/samplefile.tar.gz -O
cURL is a powerful and widely used tool, especially if you use the command line often. It has several options and supports multiple protocols.
If you want to learn more about this command, refer to the built-in manual that is available with all Unix versions:
We hope this tutorial helped you get started with cURL. Let us know how you plan to use this useful tool in the comments below.
Discover Other Linux Commands for Server Management
How to Check Disk Space on Linux
How to Calculate Process Execution With Time Command
How to Read a File With Sed Command
How to Transfer Files Using Scp Command
How to Monitor Changes With Watch Command
How to Shutdown and Restart the Server
How to Transfer and Synchronize Data With Rsync
How to Manage and List Services in Linux
How to Use the Linux Grep Command