Linux curl Command

Linux 命令大全Linux Command Encyclopedia


What is the curl command?

curl (Client URL) is a powerful command-line tool used to transfer data in Linux/Unix systems. It supports multiple protocols, including HTTP, HTTPS, FTP, SFTP, etc., and is an indispensable tool for developers and system administrators in their daily work.

Main features of curl

  1. Multi-protocol support: Supports almost all mainstream network protocols
  2. Interface-free operation: Pure command-line tool, suitable for scripts and automated tasks
  3. Feature-rich: Supports file upload/download, form submission, Cookie handling, etc.
  4. Cross-platform: Available on Linux, macOS, and Windows

Basic syntax structure

The basic syntax format of the curl command is as follows:

curl [options] [URL...]

Parameter description

  • options: Various optional parameters used to control the behavior of curl
  • URL: One or more URLs to access

Detailed explanation of common options and parameters

1. Basic request control

Option Description Example
-X Specify HTTP method curl -X POST https://example.com
-d Send POST data curl -d "name=John" https://example.com
-G Send -d data as GET parameters curl -G -d "q=keyword" https://search.com
-H Add request header curl -H "Content-Type: application/json" https://api.com

2. Output control

Option Description Example
-o Save output to file curl -o output.html https://example.com
-O Save using remote file name curl -O https://example.com/file.zip
-s Silent mode (do not show progress) curl -s https://api.com/data.json
-v Show detailed communication process curl -v https://example.com

3. Authentication and security

Option Description Example
-u Username/password authentication curl -u user:pass https://secure.com
-k Ignore SSL certificate verification curl -k https://self-signed.com
--cacert Specify CA certificate curl --cacert cert.pem https://secure.com

4. Other practical options

Option Description Example
-L Follow redirects curl -L https://short.url
-I Only get header information curl -I https://example.com
--limit-rate Limit transfer speed curl --limit-rate 100K https://largefile.com

Practical example analysis

Example 1: Get web page content

Example

curl https://www.example.com

This simplest command will fetch the content of the specified URL and display it on the terminal.

Example 2: Download files

Example

curl -O https://example.com/files/document.pdf

-OThe option will save it locally using the remote file's name (document.pdf).

Example 3: Send POST request

Example

curl -X POST -d "username=admin&password=123456" https://api.example.com/login

This command sends the username and password to the login endpoint.

Example 4: POST request with JSON data

Example

curl -X POST -H "Content-Type: application/json" \
-d '{"name":"John","age":30}' \
https://api.example.com/users

Example 5: Use Cookie to maintain session

Example

curl -c cookies.txt -b cookies.txt https://member.example.com
  • -cSave the Cookie returned by the server
  • -bSend the stored Cookie

Advanced usage

1. File upload

Example

curl -F "file=@localfile.txt" https://upload.example.com

-FUsed for multipart/form-data type form submission, commonly used for file uploads.

2. Test website response time

Example

curl -o /dev/null -s -w "Connect: %{time_connect} TTFB: %{time_starttransfer} Total: %{time_total}\n" https://example.com

This command will output:

  • Connection time
  • Time to first byte (TTFB)
  • Total time

3. Use proxy server

Example

curl -x http://proxy.example.com:8080 https://target.com

-xThe option specifies the proxy server address and port.


Frequently Asked Questions

Q1: How to solve curl certificate verification errors?

When encountering SSL certificate errors, you can temporarily use-koption to ignore verification (not recommended for production environments), or properly configure the CA certificate:

Example

curl --cacert /path/to/cert.pem https://secure-site.com

Q2: How to display request and response headers?

Use-vor-ioption:

Example

curl -v https://example.com  # Show detailed communication process
curl -i https://example.com  # Only show response headers

Q3: How to limit download speed?

Use--limit-rateoption (unit can be K, M):

Example

curl --limit-rate 200K -O https://example.com/largefile.iso

Practical exercises

  1. Try to download public JSON API data and save it to a file
  2. Use curl to test the response time of your local development server
  3. Practice sending a GET request with an authentication header
  4. Try uploading a test file to the free HTTP bin service

Example

# Practice example
curl https://httpbin.org/get
curl -o test.json https://api.github.com/users/octocat
curl -X POST -F "file=@test.txt" https://httpbin.org/post

Summary

curl is a powerful network data transfer tool in Linux systems. After studying this article, you should have mastered:

  1. Basic syntax and common options of curl
  2. Usage examples in various common scenarios
  3. Advanced features and practical tips
  4. Solutions to common problems

To learn more details, you can check the official curl documentation or useman curlView the manual page. As you accumulate practical experience, you will find that curl can accomplish far more than these basic functions.

Linux 命令大全Linux Command Encyclopedia

Other extensions