curl 命令的使用方法和各种功能。
curl 简介
curl 是一个功能强大的命令行工具,用于传输数据,支持多种协议(HTTP、HTTPS、FTP、SFTP等)。它是开发者和系统管理员最常用的网络工具之一。
基本语法
bash
curl [options] [URL...]
基础用法
1. 简单的GET请求
bash
# 获取网页内容
curl https://www.example.com
# 获取并显示响应头
curl -i https://www.example.com
# 只显示响应头
curl -I https://www.example.com
# 静默模式(不显示进度条)
curl -s https://www.example.com
2. 保存输出到文件
bash
# 使用 -o 指定输出文件名
curl -o output.html https://www.example.com
# 使用 -O 保持远程文件名
curl -O https://www.example.com/file.zip
# 下载多个文件
curl -O https://example.com/file1.zip -O https://example.com/file2.zip
3. 跟随重定向
bash
# 自动跟随重定向
curl -L https://www.example.com
# 限制重定向次数
curl -L --max-redirs 5 https://www.example.com
HTTP 方法
1. GET 请求(默认)
bash
curl https://api.example.com/users
curl -X GET https://api.example.com/users
2. POST 请求
bash
# 发送表单数据
curl -X POST -d "name=john&age=30" https://api.example.com/users
# 发送JSON数据
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"john","age":30}' \
https://api.example.com/users
# 从文件读取数据
curl -X POST \
-H "Content-Type: application/json" \
-d @data.json \
https://api.example.com/users
3. PUT 请求
bash
curl -X PUT \
-H "Content-Type: application/json" \
-d '{"name":"john","age":31}' \
https://api.example.com/users/1
4. DELETE 请求
bash
curl -X DELETE https://api.example.com/users/1
5. PATCH 请求
bash
curl -X PATCH \
-H "Content-Type: application/json" \
-d '{"age":32}' \
https://api.example.com/users/1
请求头设置
1. 设置自定义头部
bash
# 单个头部
curl -H "Authorization: Bearer token123" https://api.example.com/users
# 多个头部
curl -H "Authorization: Bearer token123" \
-H "Content-Type: application/json" \
-H "User-Agent: MyApp/1.0" \
https://api.example.com/users
# 设置空头部(移除默认头部)
curl -H "User-Agent:" https://api.example.com/users
2. 常用头部示例
bash
# JSON请求
curl -H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{"key":"value"}' \
https://api.example.com/data
# 表单请求
curl -H "Content-Type: application/x-www-form-urlencoded" \
-d "key1=value1&key2=value2" \
https://api.example.com/form
# XML请求
curl -H "Content-Type: application/xml" \
-d '<root><item>value</item></root>' \
https://api.example.com/xml
身份验证
1. Basic 认证
bash
# 用户名密码认证
curl -u username:password https://api.example.com/protected
# 只提供用户名,提示输入密码
curl -u username https://api.example.com/protected
# Base64编码的认证
curl -H "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=" https://api.example.com/protected
2. Bearer Token 认证
bash
curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." https://api.example.com/protected
3. API Key 认证
bash
# 通过头部传递
curl -H "X-API-Key: your-api-key" https://api.example.com/data
# 通过查询参数传递
curl "https://api.example.com/data?api_key=your-api-key"
文件上传
1. 表单文件上传
bash
# 上传单个文件
curl -F "file=@/path/to/file.txt" https://api.example.com/upload
# 上传多个文件
curl -F "file1=@/path/to/file1.txt" \
-F "file2=@/path/to/file2.txt" \
https://api.example.com/upload
# 指定文件类型
curl -F "file=@image.jpg;type=image/jpeg" https://api.example.com/upload
# 指定文件名
curl -F "file=@local.txt;filename=remote.txt" https://api.example.com/upload
2. 原始文件上传
bash
# 直接上传文件内容
curl -T file.txt https://api.example.com/upload
# 上传到FTP
curl -T file.txt ftp://ftp.example.com/upload/ -u username:password
3. 二进制数据上传
bash
curl -X POST \
-H "Content-Type: application/octet-stream" \
--data-binary @file.bin \
https://api.example.com/upload
Cookie 处理
1. 发送Cookie
bash
# 单个Cookie
curl -b "session_id=abc123" https://api.example.com/data
# 多个Cookie
curl -b "session_id=abc123; user_pref=dark_mode" https://api.example.com/data
# 从文件读取Cookie
curl -b cookies.txt https://api.example.com/data
2. 保存Cookie
bash
# 保存Cookie到文件
curl -c cookies.txt https://api.example.com/login
# 同时读取和保存Cookie
curl -b cookies.txt -c cookies.txt https://api.example.com/data
代理和网络设置
1. 使用代理
bash
# HTTP代理
curl --proxy http://proxy.example.com:8080 https://api.example.com/data
# SOCKS代理
curl --proxy socks5://proxy.example.com:1080 https://api.example.com/data
# 带认证的代理
curl --proxy http://username:password@proxy.example.com:8080 https://api.example.com/data
2. 网络接口和DNS
bash
# 指定网络接口
curl --interface eth0 https://api.example.com/data
# 指定DNS服务器
curl --dns-servers 8.8.8.8,8.8.4.4 https://api.example.com/data
# 强制使用IPv4或IPv6
curl -4 https://api.example.com/data # IPv4
curl -6 https://api.example.com/data # IPv6
3. 超时设置
bash
# 连接超时
curl --connect-timeout 10 https://api.example.com/data
# 总超时时间
curl --max-time 30 https://api.example.com/data
# 低速传输超时
curl --speed-time 10 --speed-limit 1000 https://api.example.com/data
SSL/TLS 设置
1. SSL选项
bash
# 忽略SSL证书错误(不推荐用于生产环境)
curl -k https://self-signed.example.com/data
# 指定CA证书
curl --cacert ca-bundle.crt https://api.example.com/data
# 使用客户端证书
curl --cert client.crt --key client.key https://api.example.com/data
# 指定SSL/TLS版本
curl --tlsv1.2 https://api.example.com/data
调试和详细输出
1. 详细信息
bash
# 显示详细的请求和响应信息
curl -v https://api.example.com/data
# 只显示头部信息
curl -I https://api.example.com/data
# 显示传输统计信息
curl -w "@curl-format.txt" https://api.example.com/data
# 内置格式化字符串
curl -w "Time: %{time_total}s\nSize: %{size_download} bytes\n" https://api.example.com/data
2. 调试选项
bash
# 跟踪请求
curl --trace trace.txt https://api.example.com/data
# ASCII格式跟踪
curl --trace-ascii trace.txt https://api.example.com/data
# 显示时间信息
curl -w "Connect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" https://api.example.com/data
高级功能
1. 范围请求(断点续传)
bash
# 下载文件的指定范围
curl -r 0-1023 https://example.com/largefile.zip
# 续传下载
curl -C - -O https://example.com/largefile.zip
2. 并行请求
bash
# 并行下载多个文件
curl -O https://example.com/file1.zip & \
curl -O https://example.com/file2.zip & \
wait
3. 配置文件
bash
# 使用配置文件
curl -K config.txt
# config.txt 内容示例:
# url = "https://api.example.com/data"
# header = "Authorization: Bearer token123"
# header = "Content-Type: application/json"
# output = "response.json"
实用脚本示例
1. API 健康检查
bash
#!/bin/bash
# health_check.sh
URL="https://api.example.com/health"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
if [ "$RESPONSE" -eq 200 ]; then
echo "API is healthy"
exit 0
else
echo "API is down (HTTP $RESPONSE)"
exit 1
fi
2. 批量API测试
bash
#!/bin/bash
# api_test.sh
BASE_URL="https://api.example.com"
TOKEN="your-token-here"
# 测试不同的端点
endpoints=("/users" "/products" "/orders")
for endpoint in "${endpoints[@]}"; do
echo "Testing $endpoint..."
curl -s -H "Authorization: Bearer $TOKEN" \
-w "Status: %{http_code}, Time: %{time_total}s\n" \
-o /dev/null \
"$BASE_URL$endpoint"
done
3. 下载进度监控
bash
# 显示下载进度
curl -L --progress-bar -o file.zip https://example.com/largefile.zip
# 自定义进度显示
curl -L -o file.zip https://example.com/largefile.zip \
-w "Downloaded: %{size_download} bytes at %{speed_download} bytes/sec\n"
常用组合示例
1. RESTful API 完整操作
bash
# 获取所有用户
curl -H "Authorization: Bearer $TOKEN" \
https://api.example.com/users
# 创建新用户
curl -X POST \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"John","email":"john@example.com"}' \
https://api.example.com/users
# 更新用户
curl -X PUT \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john.doe@example.com"}' \
https://api.example.com/users/1
# 删除用户
curl -X DELETE \
-H "Authorization: Bearer $TOKEN" \
https://api.example.com/users/1
2. 文件操作
bash
# 上传并获取响应
curl -F "file=@document.pdf" \
-F "description=Important document" \
-v \
https://api.example.com/upload
# 下载文件
curl -L -o downloaded_file.pdf \
-H "Authorization: Bearer $TOKEN" \
https://api.example.com/files/123/download
故障排查
1. 常见错误处理
bash
# 检查连接问题
curl -v --connect-timeout 5 https://api.example.com/test
# 检查DNS解析
curl -v --dns-servers 8.8.8.8 https://api.example.com/test
# 检查SSL证书
curl -vvv https://api.example.com/test 2>&1 | grep -i ssl
2. 性能分析
bash
# 创建时间分析格式文件
cat > curl-format.txt << 'EOF'
time_namelookup: %{time_namelookup}s\n
time_connect: %{time_connect}s\n
time_appconnect: %{time_appconnect}s\n
time_pretransfer: %{time_pretransfer}s\n
time_redirect: %{time_redirect}s\n
time_starttransfer: %{time_starttransfer}s\n
----------\n
time_total: %{time_total}s\n
EOF
# 使用格式文件分析性能
curl -w "@curl-format.txt" -o /dev/null -s https://api.example.com/data