跳到主要内容

Best Practices of Using curl

· 阅读需 4 分钟

Introduction​

curl is a powerful command-line tool used for transferring data with URLs. It supports a wide range of protocols, including HTTP, HTTPS, FTP, and more. This article provides a comprehensive guide to the best practices of using curl, covering various scenarios such as checking if an URL is alive, downloading files, making HTTP GET/POST requests, handling authentication, and dealing with HTTPS certificates.


Basic Usage​

Check if an URL is Alive​

To check if an URL is alive, you can use the -I (or --head) option to send a HEAD request:

curl -I https://example.com

This command will return the HTTP headers without downloading the content, allowing you to quickly check if the URL is accessible.

Download a File​

To download a file from a URL, simply use the curl command followed by the URL:

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

The -O option saves the file with the same name as the remote file. If you want to save the file with a different name, use the -o option:

curl -o myfile.zip https://example.com/file.zip

HTTP Methods​

HTTP GET​

To make an HTTP GET request, use the curl command followed by the URL:

curl https://example.com/api/resource

HTTP POST with JSON Payload​

To make an HTTP POST request with a JSON payload, use the -X POST option and specify the JSON data using the -d option:

curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://example.com/api/resource

Authentication​

HTTP Basic Authentication​

To use HTTP Basic Authentication, use the -u option to specify the username and password:

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

Bearer Token Authentication​

To use Bearer Token Authentication, use the -H option to set the Authorization header:

curl -H "Authorization: Bearer YOUR_TOKEN" https://example.com/api/resource

HTTPS and Certificates​

HTTPS with Certificate​

To use HTTPS with a client certificate, use the --cert and --key options:

curl --cert client.crt --key client.key https://example.com/api/resource

Ignore HTTPS Certificate​

To ignore HTTPS certificate validation (not recommended for production), use the -k (or --insecure) option:

curl -k https://example.com/api/resource

Advanced Scenarios​

Custom Headers​

To send custom headers, use the -H option:

curl -H "X-Custom-Header: value" https://example.com/api/resource

Follow Redirects​

To follow HTTP redirects, use the -L (or --location) option:

curl -L https://example.com/redirect

Verbose Output​

To get verbose output, use the -v (or --verbose) option:

curl -v https://example.com/api/resource

Timeouts​

To set a timeout for the request, use the --connect-timeout and --max-time options:

curl --connect-timeout 5 --max-time 10 https://example.com/api/resource

Conclusion​

curl is a versatile and powerful tool for interacting with web services and downloading files from the command line. By mastering the various options and scenarios covered in this article, you can effectively use curl for a wide range of tasks, from simple URL checks to complex HTTP requests with authentication and custom headers.


References​

  1. curl Documentation
  2. curl Man Page
  3. HTTP Methods
  4. HTTP Authentication
  5. HTTPS

By following these best practices, you can leverage curl to its full potential and efficiently manage your web interactions from the command line.