乐闻世界logo
搜索文章和话题

cURL 的 -v、-i、-I、-s 参数有什么区别?

3月6日 23:06

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

标签:cURL