Learn How to Use the curl Command in Linux for HTTPS Requests (2024)

Curl (Client URL) is a command-line utility on Linux and other operating systems provided by the libcurl library. It is a free and open-source client-side URL transfer library for transferring data using various network protocols. Therefore, its ability to support multiple protocols, including HTTP, HTTPS, FTP, SFTP, and more, makes it an excellent tool for fetching data, testing endpoints, and debugging secure connections. When working with HTTPS, Curl ensures secure communication using SSL/TLS protocols.

On the Page hide

Installing Curl

Basic Syntax of Curl

Typical curl Commands for HTTPS

1. Fetch a Webpage

2. Download a File

3. Save Output to a File

5. Follow Redirects

6. Test HTTPS Connection with Detailed Information

7. Use a Specific Certificate

8. Specify SSL/TLS Versions

9. Ignore SSL Certificate Validation (Not Recommended)

Advanced HTTPS Features

1. Test HTTP Authentication

2. Post Data to a Secure Server

3. Pass Custom Headers

4. Measure Download Speed

Debugging HTTPS with Curl

1. Check Certificate Details

2. Test Connection to a Specific Port

3. Check Supported Cipher Suites

Advantages of Using Curl to test HTTPS

Conclusion

Installing Curl

cURL would not be available by default on all Linux systems; therefore, we need to install it manually on our distro. Here, we are giving commands to install cURL on popular Linux distros based on Debian/Ubuntu, Arch Linux, or RHEL.

On Debian/Ubuntu

sudo apt updatesudo apt install curl

On RHEL/AlmaLinux/Fedora

sudo dnf install curl

On Arch Linux

sudo pacman -S curl

Verify the installation: To confirm it is on your system:

curl --version

Basic Syntax of Curl

Once you have installed the cURL on the Linux system, the basic command syntax to use it is:

curl [options] [URL]

For HTTPS: The given syntax is the command line way to connect to and get data from a website over the HTTPS protocol.

curl https://example.com

Typical curl Commands for HTTPS

To use the cURL specifically for HTTPS, here are the syntaxes to follow:

1. Fetch a Webpage

If anyone wants to use the command terminal to confirm that a webpage is working and the system can connect it over an HTTPS URL, can use the given command syntax. It will fetch and display the website content in HTML format.

curl https://example.com
Learn How to Use the curl Command in Linux for HTTPS Requests (1)

2. Download a File

We can also download files available on web pages using HTTPS (443). Let’s say an application’s zip file needs to be downloaded using a command terminal and CURL. Here is the command to do that. The -O flag saves the file with its original name.

curl -O https://domain.com/file.zip

3. Save Output to a File

We have seen how to use cURL to download a file. Now, if you want to save the file but with a custom name on your system, the—ooption allows you to specify a custom filename.

curl -o output.html https://domain.com/file.zip

4. Display HTTPS Headers

HTTPS headers are key-value pairs sent between a client (e.g., a web browser) and a server during an HTTPS request or response. These headers provide metadata about the request or response, such as content type, length, caching directives, server information, authentication tokens, and more. To check the HTTPS headers using the cURL command, we can use the “-I” option to fetch only the HTTP headers.

curl -I https://example.com
Learn How to Use the curl Command in Linux for HTTPS Requests (2)

5. Follow Redirects

If a URL has a redirect, the standard curl command will not fetch the webpage to read its content due to redirection. So, to ensure the cURL command automatically follows the redirects to get the working website, we need to use the “-L” option in the command.

curl -L https://example.com

For example, in thescreenshot, we first used the standard curl command, which gave HTTP response code301, meaning the document had been moved to anotherplace. Therefore, to follow the redirect, we used -L in the same command, and this time, cURL could fetch the moved page successfully by following the redirect.

Learn How to Use the curl Command in Linux for HTTPS Requests (3)

6. Test HTTPS Connection with Detailed Information

cURL can also be used to test the HTTPS connections because it can show the detailed process of connecting the remote webpage, including the SSL handshakes. We need to use the “-v” option in the cURL command; here is the example:

curl -v https://example.com

7. Use a Specific Certificate

Using specific certificates with Curl allows secure communication with HTTPS servers that require client-side authentication. This is often necessary for APIs or services requiring proof of identity from the client. --cert client.pem: It specifies the client certificate (in .pem format), used to authenticate the client to the server.
--key key.pem: It specifies the private key corresponding to the client certificate, which proves ownership of the certificate.

curl --cert client.pem --key key.pem https://example.com

8. Specify SSL/TLS Versions

We can force a webpage to use a specific SSL or TLS version and check if it is working fine with our website; for that, we need to use the SSL version in the command that we want to use to test for the webpage. For example, the given command will use the “–tlsv1.2″ flag to force Curl to use TLS 1.2.

curl --tlsv1.2 https://example.com

9. Ignore SSL Certificate Validation (Not Recommended)

The -k option bypasses SSL certificate validation, which is helpful in testing servers with self-signed certificates.

curl -k https://example.com

Advanced HTTPS Features

1. Test HTTP Authentication

If the website is in the testing phase or private and requires ausernameandpasswordto access it, we can use the “-u” option with cURL to test such web pages. This option lets users define basic authentication credentials in the command to fetch the HTML data.

curl -u username:password https://example.com

2. Post Data to a Secure Server

To send data to a secure server with Curl, we can use the -X option to specify the HTTP method (e.g., POST) and the -d option to define the data payload to be sent in the request.

curl -X POST -d "key=value" https://example.com

Additional Options to use with -X and POST:

Set headers (e.g., Content-Type):

curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com

Specify authentication:

curl -X POST -u username:password -d "key=value" https://example.com

Enable verbose mode for debugging:

curl -v -X POST -d "key=value" https://example.com

3. Pass Custom Headers

The -H option in Curl allows us to pass custom headers to our HTTP requests, which is particularly useful for sending authentication tokens, setting content types, or any other additional information required by a server.

curl -H "Authorization: Bearer token" https://example.com

4. Measure Download Speed

The curl command with the -w flag can measure download speed and other performance metrics for a request. Here’s how it works:

curl -w "Download Speed: %{speed_download}\n" -o /dev/null https://example.com

Debugging HTTPS with Curl

1. Check Certificate Details

Displays detailed information about the SSL certificate and TLS handshake.

curl -v https://example.com

2. Test Connection to a Specific Port

Specify the port explicitly if the server uses a non-standard HTTPS port.

curl https://example.com:8443

3. Check Supported Cipher Suites

Test if a server supports specific SSL/TLS cipher suites.

curl --ciphers AES256-GCM-SHA384 https://example.com

Advantages of Using Curl to test HTTPS

  • It is a cross-platform open-source tool that can be used for free on Linux, macOS, and Windows.
  • Users can easily integrate the cURL in their scripts to perform automation.
  • cURL supports modern SSL/TLS protocols for encrypted communication.
  • It required minimal resources.
  • It offers a wide range of options or flags and is, hence, highly configurable.

Conclusion

We have tried to cover all common cURL commands developers or system administrators require to test and interact with HTTPS connections in Linux. Because of its versatility, this tool is quite popular among sysadmins and network engineers. By grasping cURL commands well, users can efficiently troubleshoot HTTPS-related issues and manage secure connections.

Other Articles:

  • 8 Top Commands for Monitoring CPU Utilization in Linux
  • AlmaLinux / Rocky Linux open HTTP/HTTPS port 80/443 with firewalls
  • How to Install and Use arp Command on Linux with Example
  • How do you Turn off Password Authentication in SSH?
Learn How to Use the curl Command in Linux for HTTPS Requests (2024)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Corie Satterfield

Last Updated:

Views: 5497

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.