Master the cURL command with these 10 examples

Master the cURL command with these 10 examples

When it comes to retrieving information about a Web application from the command line, whether on Linux, Windows or macOS, the cURL tool is simply indispensable.. It's a free tool, useful for both developers and sysadmins. Here's a selection of examples to get you started!

cURL is often used to query HTTP and HTTPS protocols, but it also supports a wide range of protocols, as specified on the official website: DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP.

Did you know that cURL stands for Client URL?

1. How do I install cURL on Debian?

While it is integrated into Windows 10, Windows 11 and recent versions of Windows Server, on Linux, cURL must be installed via the package manager. For example, on Debian 11, this results in :

sudo apt-get update
sudo apt-get install curl

The installation is very simple, but I wanted to clarify something.

2. Retrieving page content with cURL

cURL's primary mission is to retrieve information from a Web server. So, if you invoke the command "curl" followed by a domain name or Web address, the page's source code will be returned. Give it a try:

curl tutobox.fr

The console displays the source code for the Tutobox home page. If the address cannot be found, the following message appears:

curl: (6) Could not resolve host: http://tutibox.fr

Now let's explore cURL's options...

3. Storing page content in a file with cURL

If you need to store the contents of a page in an output file, you can use cURL. In the example below, the source code of the page " https://tutobox.fr "will be stored in the " /home/tutobox/tutobox.co.uk.html" .

curl https://tutobox.fr > /home/tutobox/tutobox.co.uk.html
Example cURL

4. Display HTTP request and response with cURL

When connecting to a page using the HTTP (or HTTPS) protocol, the HTTP client issues an HTTP request and the server returns an HTTP response, with various information including the page content. To view both the request and the response with cURL, the "-v" option must be used.

curl -v https://tutobox.fr

5. Display HTTP header with cURL

In an HTTP response, the server header contains important information: the HTTP version used, the return code (e.g. "200 OK" when the request is processed successfully), the date and time, the server type, etc. Sometimes, the page content isn't of interest and we try to analyze the HTTP header only.

To retrieve the header only, use the "-head" option or its shortcut "-I". For example:

curl --head https://tutobox.fr
curl -I https://tutobox.fr
cURL - HTTP header

6. Using cURL through a proxy

Need to use a proxy to access the Internet? Not a problem, since cURL supports the use of a proxy. Use the "-proxy" option, followed by the proxy address and port (separated by ":") to specify the information.

curl --proxy : https://tutobox.fr

7. cURL: use a different user-agent

When connecting to a Web server using the HTTP(S) protocol, the HTTP request contains a field called "User-Agent", which allows the HTTP client to introduce itself to the server. With cURL, the " User-Agent "is" curl/7.74.0 "a way of saying Hello, my name is cURL and I would like to make a request.... ".

Each software can have its own value: Mozilla Firefox, Google Chrome, Microsoft Edge, etc... But also other software such as Nikto, a vulnerability scanner for websites. If you want to test whether a website blocks Nikto, you can ask cURL to use "Nikto" as the value for the User-Agent.

This gives the following command, if you also want to display only the header:

curl -I tutobox.fr -H "User-Agent: Nikto"

Here we can see that access is denied by return code 403 :

cURL - Change user-agent

8. Ignore certificate error with cURL

When cURL detects a certificate error on a Web page, it is not very happy. However, this is often the case on the administration pages of a network device, a local site, etc... Because of the self-signed certificate. With the "-insecure" option, you can ignore this error.

curl --insecure https://192.168.1.254

9. Download a file with cURL

In addition to displaying the content of a Web page, cURL can be used to download a file: a video, a script, an image, etc. It is very common to use it to download a script from GitHub. You can also use cURL to upload a file to an FTP server, authenticating with a username and password.

Here is an example for FTP:

curl -u : -O ftp://serveur-ftp.tutobox.fr/video.avi

Here's an example of how to upload a script to GitHub:

curl -O https://raw.githubusercontent.com/tutobox/script.sh

10. Using cURL with a specific SSL/TLS version

When configuring a website, you can enable support for one or more versions of SSL/TLS. To ensure compatibility with older browsers, certain protocols need to be enabled. Conversely, to secure your site, you may want to disable certain versions. With cURL, you can attempt a connection with a specific version of the SSL/TLS protocols, allowing you to test your configuration.

curl --sslv3 https://tutobox.fr
curl --tlsv1 https://tutobox.fr
curl --tlsv1.0 https://tutobox.fr
curl --tlsv1.1 https://tutobox.fr
curl --tlsv1.2 https://tutobox.fr
curl --tlsv1.3 https://tutobox.fr

So you may get an error, or a correct response, depending on the protocol version specified and the configuration of the server in front of you.

11. Querying several sites at once with cURL

Let's finish with one last tip: the ability to query several sites, or addresses if you prefer, at the same time with cURL. For example, to target "tutabox.co.uk", "tutibox.co.uk" and "tutebox.co.uk", rather than making the requests one by one, you can do :

curl https://{tutabox,tutibox,tutebox}.fr
cURL - Multiple URLs

12. Conclusion

After reading this article, you'll be able to get to grips with the cURL tool! Want to test even more cURL commands? See advanced help :

curl --help all

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *