Linux curl Command
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
- Multi-protocol support: Supports almost all mainstream network protocols
- Interface-free operation: Pure command-line tool, suitable for scripts and automated tasks
- Feature-rich: Supports file upload/download, form submission, Cookie handling, etc.
- 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 curlURL: 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
This simplest command will fetch the content of the specified URL and display it on the terminal.
Example 2: Download files
Example
-OThe option will save it locally using the remote file's name (document.pdf).
Example 3: Send POST request
Example
This command sends the username and password to the login endpoint.
Example 4: POST request with JSON data
Example
-d '{"name":"John","age":30}' \
https://api.example.com/users
Example 5: Use Cookie to maintain session
Example
-cSave the Cookie returned by the server-bSend the stored Cookie
Advanced usage
1. File upload
Example
-FUsed for multipart/form-data type form submission, commonly used for file uploads.
2. Test website response time
Example
This command will output:
- Connection time
- Time to first byte (TTFB)
- Total time
3. Use proxy server
Example
-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
Q2: How to display request and response headers?
Use-vor-ioption:
Example
curl -i https://example.com # Only show response headers
Q3: How to limit download speed?
Use--limit-rateoption (unit can be K, M):
Example
Practical exercises
- Try to download public JSON API data and save it to a file
- Use curl to test the response time of your local development server
- Practice sending a GET request with an authentication header
- Try uploading a test file to the free HTTP bin service
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:
- Basic syntax and common options of curl
- Usage examples in various common scenarios
- Advanced features and practical tips
- 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 Command Encyclopedia