curl 是一个强大的命令行工具,用于传输数据,支持多种协议(HTTP、HTTPS、FTP 等)。以下是详细用法:
一、基本语法
curl [options] [URL]
二、常用选项分类
1. 请求方法
# GET(默认)
curl https://api.example.com/data
# POST
curl -X POST https://api.example.com/data
curl -X POST -d "name=value" https://api.example.com
# PUT
curl -X PUT https://api.example.com/data
# DELETE
curl -X DELETE https://api.example.com/data
# PATCH
curl -X PATCH https://api.example.com/data
2. 发送数据
# 发送表单数据
curl -d "param1=value1¶m2=value2" https://example.com
# 发送JSON数据
curl -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com
# 发送文件内容
curl -d @data.json https://example.com
# 发送multipart/form-data(文件上传)
curl -F "file=@/path/to/file" -F "name=test" https://example.com/upload
3. 设置请求头
# 单个请求头
curl -H "Authorization: Bearer token123" https://api.example.com
# 多个请求头
curl -H "Content-Type: application/json" -H "X-API-Key: abc123" https://api.example.com
# 自定义User-Agent
curl -A "Mozilla/5.0" https://example.com
4. 处理响应
# 显示响应头
curl -i https://example.com # 包含响应头
curl -I https://example.com # 仅响应头(HEAD请求)
# 保存响应到文件
curl -o output.html https://example.com
curl -O https://example.com/file.zip # 使用远程文件名
# 显示详细过程
curl -v https://example.com # 显示请求/响应详细信息
curl --trace-ascii debug.txt https://example.com
# 静默模式
curl -s https://example.com # 不显示进度和错误信息
curl -sS https://example.com # 静默但显示错误
5. 认证相关
# 基本认证
curl -u username:password https://example.com
# Bearer Token认证
curl -H "Authorization: Bearer token123" https://api.example.com
# Cookie
curl -b "name=value" https://example.com # 发送Cookie
curl -c cookies.txt https://example.com # 保存Cookie到文件
curl -b cookies.txt https://example.com # 从文件读取Cookie
6. 网络设置
# 超时设置
curl --connect-timeout 10 https://example.com # 连接超时10秒
curl --max-time 30 https://example.com # 总超时30秒
# 跟随重定向
curl -L https://example.com
curl --max-redirs 5 https://example.com # 限制重定向次数
# 代理设置
curl -x http://proxy.example.com:8080 https://target.com
curl --proxy-user user:pass -x http://proxy:8080 https://target.com
7. SSL/TLS设置
# 跳过证书验证(测试用)
curl -k https://example.com
# 指定客户端证书
curl --cert client.pem --key key.pem https://example.com
# 指定CA证书
curl --cacert ca.pem https://example.com
8. 下载控制
# 断点续传
curl -C - -O https://example.com/largefile.zip
# 限速
curl --limit-rate 100k -O https://example.com/file.zip
# 并发下载多个文件
curl -O https://example.com/file1.zip -O https://example.com/file2.zip
三、实用示例
1. API测试
# GET请求
curl "https://api.github.com/users/octocat"
# POST JSON数据
curl -X POST -H "Content-Type: application/json" \
-d '{"title":"Test","body":"Content"}' \
https://api.example.com/posts
# 带认证的API调用
curl -H "Authorization: token YOUR_TOKEN" \
https://api.github.com/user/repos
2. 文件操作
# 上传文件
curl -F "file=@/path/to/local/file" https://example.com/upload
# 下载文件并显示进度条
curl -# -O https://example.com/file.zip
# 下载文件到指定目录
curl -o /tmp/file.zip https://example.com/file.zip
3. 调试和测试
# 查看请求详情
curl -v -X POST -d "test=data" https://example.com
# 测试响应时间
curl -w "time_total: %{time_total}\n" https://example.com
# 获取HTTP状态码
curl -o /dev/null -s -w "%{http_code}\n" https://example.com
4. 批量操作
# 从文件读取多个URL
curl -K urls.txt
# urls.txt内容示例:
# url = "https://example.com/1"
# output = "file1.txt"
# url = "https://example.com/2"
# output = "file2.txt"
# 使用通配符
curl -O "https://example.com/files/file[1-3].txt"
四、高级技巧
1. 格式化输出
# 使用jq处理JSON响应
curl -s https://api.example.com/data | jq '.'
# 自定义输出格式
curl -w "DNS: %{time_namelookup} Connect: %{time_connect}\n" https://example.com
2. 环境变量
# 使用环境变量存储敏感信息
export API_TOKEN="your_token"
curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com
3. 脚本中使用
#!/bin/bash
response=$(curl -s https://api.example.com/data)
if [ $? -eq 0 ]; then
echo "Success: $response"
else
echo "Failed"
fi
五、常用变量(-w选项)
# 可用的变量:
%{http_code} # HTTP状态码
%{time_total} # 总时间
%{time_connect} # 连接时间
%{time_namelookup}# DNS时间
%{size_download} # 下载字节数
%{speed_download} # 下载速度
%{url_effective} # 最终URL(跟随重定向后)
六、注意事项
- 安全性:不要在命令行中直接暴露密码,使用环境变量或配置文件
- 证书验证 :生产环境不要使用
-k选项 - 性能 :大量请求时考虑使用连接复用(
--keepalive-time) - 编码:注意URL编码,特殊字符需要转义
七、查看帮助
curl --help # 简要帮助
man curl # 完整手册
curl --manual # 详细手册
八、实战
curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: Bearer token" \
-d "method=c_&tags=\\A290213PT0A008840" \
http://tzy.com/mxywebapi/webapi/PSpaceQuery.ashx
curl -X POST "http://tzy.com/auth/dreamCloudReturn" \
-H "Content-Type: application/json" \
-d '{
"applyClientId": "w20thf500dom",
"jwt": "token"
}'