cURL 提供了多个输出控制参数,用于控制请求的详细程度和输出格式。理解这些参数的区别对于调试和日常使用非常重要。
参数对比表
| 参数 | 全称 | 作用 | 输出内容 |
|---|---|---|---|
-v | --verbose | 详细模式 | 完整请求/响应信息 |
-i | --include | 包含响应头 | 响应头 + 响应体 |
-I | --head | 仅请求头 | 仅响应头(HEAD 请求) |
-s | --silent | 静默模式 | 仅响应体(无进度) |
-S | --show-error | 显示错误 | 配合 -s 使用 |
详细说明和示例
1. -v(verbose)详细模式
bash# 显示完整的请求和响应过程 curl -v https://api.example.com # 输出示例: # * Trying 192.168.1.1:443... # * Connected to api.example.com (192.168.1.1) port 443 (#0) # > GET / HTTP/1.1 # > Host: api.example.com # > User-Agent: curl/7.68.0 # > Accept: */* # > # * Mark bundle as not supporting multiuse # < HTTP/1.1 200 OK # < Content-Type: application/json # < Content-Length: 123 # < # {"status":"ok","data":[]} # * Connection #0 to host api.example.com left intact
输出符号说明:
*- 连接和协议信息>- 发送的请求数据<- 接收的响应数据{或}- SSL/TLS 握手信息
2. -i(include)包含响应头
bash# 显示响应头和响应体 curl -i https://api.example.com # 输出示例: # HTTP/1.1 200 OK # Content-Type: application/json # Content-Length: 123 # Date: Mon, 01 Mar 2026 10:00:00 GMT # # {"status":"ok","data":[]}
3. -I(head)仅响应头
bash# 发送 HEAD 请求,仅获取响应头 curl -I https://api.example.com # 输出示例: # HTTP/1.1 200 OK # Content-Type: application/json # Content-Length: 123 # Date: Mon, 01 Mar 2026 10:00:00 GMT # Last-Modified: Sun, 28 Feb 2026 08:00:00 GMT # ETag: "abc123" # Cache-Control: max-age=3600
注意: -I 会自动发送 HEAD 请求,不会获取响应体。
4. -s(silent)静默模式
bash# 静默模式,不显示进度信息 curl -s https://api.example.com # 仅输出响应体内容 # {"status":"ok","data":[]} # 配合 -S 显示错误信息 curl -s -S https://api.example.com # 配合 -o 输出到文件 curl -s https://api.example.com -o output.json
组合使用
bash# 静默 + 显示错误 curl -s -S https://api.example.com # 详细 + 输出到文件 curl -v https://api.example.com -o output.json 2>debug.log # 包含响应头 + 静默 curl -i -s https://api.example.com # 仅响应头 + 格式化 curl -I -s https://api.example.com | grep -i content-type
实际应用场景
场景 1:API 调试
bash# 查看完整的请求响应过程 curl -v -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name":"test"}'
场景 2:检查响应头
bash# 检查缓存头 curl -I -s https://cdn.example.com/image.jpg | grep -i cache # 检查 Content-Type curl -I -s https://api.example.com/data | grep -i content-type
场景 3:脚本中使用
bash# 静默获取数据并解析 response=$(curl -s https://api.example.com/users) echo "$response" | jq '.[] | .name' # 检查 HTTP 状态码 status=$(curl -s -o /dev/null -w "%{http_code}" https://api.example.com) if [ "$status" = "200" ]; then echo "OK" else echo "Error: $status" fi
场景 4:调试 SSL 问题
bash# 查看 SSL 握手过程 curl -v https://api.example.com 2>&1 | grep -E "(SSL|TLS|certificate)" # 保存详细日志用于分析 curl -v https://api.example.com 2>ssl-debug.log
其他相关参数
bash# --trace-ascii 保存详细跟踪信息 curl --trace-ascii debug.txt https://api.example.com # --trace-time 添加时间戳 curl -v --trace-time https://api.example.com # -w 自定义输出格式 curl -s -o /dev/null -w "Status: %{http_code}\nTime: %{time_total}s\n" https://api.example.com # --progress-bar 显示进度条 curl --progress-bar -O https://example.com/large-file.zip
总结建议
| 场景 | 推荐参数 |
|---|---|
| 日常调试 | -v |
| 查看响应头 | -I 或 -i |
| 脚本中使用 | -s 或 -s -S |
| 性能测试 | -s -o /dev/null -w ... |
| 下载文件 | -s -O 或 --progress-bar |