Limited Time Free!  Sign up for 1TB of data transfer and get free trials of WAF and Bot Management!
Get Started Now 
Tencent EdgeOne Blog
Tencent EdgeOne Blog
Tutorial

Navigating the World of cURL Command: A Comprehensive Guide

Tencent EdgeOne - Product Manager

how to test using curl

In the expansive realm of web development and data communication, the cURL command serves as a potent vessel, enabling the transfer of data to or from a server using multiple protocols. This versatile command-line tool is the preferred choice for developers requiring testing websites, be it for testing APIs, verifying file downloads, or automating web testing.

What is cURL Command?

cURL stands for Client URL, which is a software project providing a library and command-line tool for transferring data using URL syntax. It supports a wide range of protocols, including HTTP, HTTPS, FTP, SFTP, and more, and can meet almost all network request needs. The cURL command's ability to handle everything from simple HTTP requests to complex operations like HTTP POST, cookies, authentication, and more, underscores its utility in web development and system administration.

How to Use cURL Command?

Using the cURL command is straightforward but powerful. In general, you can test cURL commands using the command line tool that comes with your computer. For example, in Windows, press "Win + R", then type cmd to open the command line tool (on a Mac, press "Command + Space", type "terminal" and press Enter), then type the cURL command (such as curl https://example.com) and press "Enter", it will return the response data to you.

This fetches the content available at the given URL, and prints it onto the terminal. For example, if you run curl example.com, you should be able to see the HTML page printed, as shown below:

curl example

What are the Options for cURL Command?

cURL boasts a vast array of options that cater to different needs, some of the notable options include:

  • `-o` or `--output`: Saves the output to a file instead of displaying it in the terminal.
  • `-I` or `--head`: Fetches the headers of a specified URI.
  • `-X` or `--request`: Specifies a custom request method to use.
  • `-d` or `--data`: Sends the specified data in a POST request to the server.
  • `-u` or `--user`: Passes the user authentication details.

The full list of options is extensive, and you can consult the cURL manual or help command (`curl --help` or `man curl`) for a comprehensive understanding.

What are Commonly Used cURL Commands?

Several cURL commands stand out for their frequent utility:

 1. Get webpage content:
 Use the curl [URL] command to get webpage content. This command will display the HTML content of the https://example.com webpage. Example:

curl https://example.com

2. Send GET request:
Use curl -X GET [URL] to send a GET request to https://example.com. This is commonly used to request query information. Example:

curl -X GET https://example.com

3. Send POST request:
Use the POST method to submit data, which is commonly used for form submission. Use curl -X POST -d "data" [URL] to send a POST request. Example:

curl -X POST -d "param1=value1&param2=value2" https://example.com/login

4. Upload file:
Use curl -F "file=@[file_path]" [URL] to upload to the specified server address. Example:

curl -F "file=@path/to/local/file" https://example.com/upload

5. Download file:
Use curl -o [URL] to download files. Download the remote filename file to local. Example:

curl -o filename https://example.com/file

6. Set request header:
Use curl -H "header: Value" [URL] to set the request header. Example:

curl -H "Content-Type: aplication/json" https://example.com

7. Save and use cookies:
Save session Cookies: curl -c cookies.txt [URL] can save session Cookies to local files when visiting websites. Example:

curl -c cookies.txt https://example.com

8. Handle redirects:
Automatically handle redirects: curl -L [URL], check redirect links: curl -i [URL]. Example:

curl -L https://example.com

9. Set timeout:
Set connection and response timeout: curl --connect-timeout [seconds] --max-time [seconds] [URL]. Example:

curl -m 10 https://example.com

How to Use cURL Command to Check SSL Certificate?

To use cURL to check an SSL certificate, you can use the --verbose or -v option, which will display detailed information about the SSL/TLS handshake, including information about the certificate and its validity.

Here's the basic syntax for using cURL to check an SSL certificate:

curl -v https://example.com

Replace example.com with the domain you want to check. When you run this command, you'll see output similar to the following:

use curl to check ssl certificateIn this output, you can see information about the server certificate, such as the subject, start date, expire date, and issuer. The line SSL certificate verify ok indicates that the certificate is valid and the SSL connection is properly established.

If you want to retrieve only the certificate information, you can use the --insecure or -k option to disable certificate verification and use the --cert-status option to retrieve the certificate status:

curl -k --cert-status https://example.com

This command will output the certificate information without verifying it. However, keep in mind that using the --insecure option might expose you to security risks, as it disables SSL/TLS certificate validation.

You can also use our SSL Certificate Checker tool, which is available online for free and can quickly check a website's SSL certificate.

How to Use cURL Command to Check Http Status?

To use cURL to check the HTTP status of a website, you can use the -I or --head option to fetch only the headers, or the -w or --write-out option to customize the output. Here are two methods to check the HTTP status using cURL:

Method 1: Using -I or --head option:

curl -I https://example.com

This command will fetch the headers of the specified URL and display them in the terminal. The first line of the output will show the HTTP status, like this:

use curl to check http headers

In this example, the HTTP status is 200, which means the request was successful.

Method 2: Using -w or --write-out option:

curl -o /dev/null -s -w "%{http_code}\n" https://example.com

This command will:

  • Use -o /dev/null to redirect the output body to /dev/null, effectively discarding it.
  • Use -s or --silent to suppress the progress bar and error messages.
  • Use -w "%{http_code}\n" to display only the HTTP status code followed by a newline character.

The output will show only the HTTP status code, like this:

use curl to check http status

We also offer an free online Redirect Checker tool that can quickly check a website's HTTP redirect status codes.

Conclusion

In summary, the cURL command is an essential tool for web professionals as it offers unmatched flexibility and extensive capabilities for handling online data transfer tasks. Whether you're downloading files, testing APIs, or retrieving HTTP headers of a webpage, cURL's wide range of options and powerful command-line utility make it a valuable tool for web developers, system administrators, and tech enthusiasts.

If you have configured Site Acceleration, Certificates, Cache Control, and other configurations on Tencent EdgeOne, you can quickly verify whether the global configuration takes effect using cURL. If you have defined separate configurations for the domain through the Rule Engines, cURL can still effectively verify the results.

If you are interested in experiencing Tencent EdgeOne and its features firsthand, feel free to Contact Us to learn more information. You can also Click Here to get started free and experience its benefits.

Develop
Web