curl 命令详解
以下是 curl 命令的详细解析,涵盖常用选项、功能分类及实际示例,帮助您全面掌握这个强大的网络工具:
1. 基础语法
curl [options] [URL]
2. 核心功能与常用选项
(1)请求控制
| 选项 | 作用 | 示例 |
|---|---|---|
-X <METHOD> |
指定HTTP方法(GET/POST/PUT等) | curl -X POST https://api.com |
-H "Header: Val" |
添加请求头 | curl -H "Content-Type: application/json" URL |
-d "data" |
发送POST数据(默认Content-Type: application/x-www-form-urlencoded) | curl -d "name=John" URL |
--data-raw |
发送原始数据(不处理特殊字符) | curl --data-raw '{"key":"value"}' URL |
--data-binary |
发送二进制数据(保留换行符等) | curl --data-binary @file.txt URL |
-F "name=content" |
上传文件(multipart/form-data) | curl -F "file=@test.jpg" URL |
(2)输出控制
| 选项 | 作用 | 示例 |
|---|---|---|
-o <file> |
将输出保存到文件 | curl -o output.html URL |
-O |
使用远程文件名保存 | curl -O http://site.com/file.txt |
-s |
静默模式(不显示进度/错误) | curl -s URL |
-v |
显示详细通信过程(调试用) | curl -v URL |
--trace <file> |
将完整通信过程记录到文件 | curl --trace debug.txt URL |
(3)连接控制
| 选项 | 作用 | 示例 |
|---|---|---|
--connect-timeout <sec> |
连接超时时间 | curl --connect-timeout 5 URL |
-m <sec> |
最大请求时间 | curl -m 10 URL |
--retry <num> |
失败重试次数 | curl --retry 3 URL |
--compressed |
要求服务器返回压缩数据 | curl --compressed URL |
(4)认证与安全
| 选项 | 作用 | 示例 |
|---|---|---|
-u user:pass |
基本认证 | curl -u admin:12345 URL |
--ssl |
尝试使用SSL/TLS | curl --ssl URL |
--cacert <file> |
指定CA证书 | curl --cacert cert.pem URL |
-k/--insecure |
忽略SSL证书验证(不安全,仅测试用) | curl -k https://expired.badssl.com |
(5)代理与网络
| 选项 | 作用 | 示例 |
|---|---|---|
-x <proxy> |
使用代理服务器 | curl -x http://proxy:8080 URL |
--socks5 <host> |
SOCKS5代理 | curl --socks5 localhost:9050 URL |
--interface <if> |
指定网络接口 | curl --interface eth0 URL |
(6)高级功能
| 选项 | 作用 | 示例 |
|---|---|---|
-L |
自动跟随重定向 | curl -L URL |
-D <file> |
将响应头保存到文件 | curl -D headers.txt URL |
-c <file> |
保存Cookies到文件 | curl -c cookies.txt URL |
-b <file> |
从文件加载Cookies | curl -b cookies.txt URL |
-A "User-Agent" |
设置User-Agent头 | curl -A "Mozilla/5.0" URL |
3. 实用组合示例
(1)调试API请求
curl -v -X POST https://api.example.com/users \
-H "Authorization: Bearer token123" \
-H "Content-Type: application/json" \
-d '{"name":"John", "age":30}'
(2)下载文件并续传
curl -C - -O http://example.com/largefile.zip
(3)模拟浏览器访问
curl -A "Mozilla/5.0" -H "Accept-Language: en-US" -L https://example.com
(4)测试网站响应时间
curl -s -w "DNS: %{time_namelookup} | Connect: %{time_connect} | Total: %{time_total}\n" -o /dev/null https://example.com
4. 特殊场景处理
(1)处理JSON响应
curl -s https://api.example.com/data | jq . # 需要安装jq工具
(2)上传多个文件
curl -F "file1=@a.jpg" -F "file2=@b.pdf" http://upload.com
(3)通过代理访问
curl -x socks5h://localhost:1080 https://google.com
5. 完整选项列表
查看所有支持的选项:
curl --help all
或查阅手册:
man curl
6. 注意事项
-
敏感信息:避免在命令行直接暴露密码(使用
-u user:交互输入密码)。 -
HTTPS安全:生产环境不要使用
-k跳过证书验证。 -
速率限制:添加
--limit-rate 1M限制下载速度。
掌握这些选项后,您可以高效完成HTTP调试、文件传输、API测试等任务。根据实际需求组合使用,能覆盖90%以上的网络操作场景。
------------------------------------------------------------------------------------------------------------
1. 命令作用
curl -I(或 --head)用于 仅获取HTTP响应头,不下载正文内容。
典型用途:
-
检查服务器状态(如状态码、重定向)
-
验证资源是否存在(无Body传输,节省带宽)
-
查看响应头信息(Content-Type、缓存控制等)
2. 完整语法
curl -I [URL]
# 等效于
curl --head [URL]
3. 关键输出字段解析
执行 curl -I https://example.com 示例输出:
HTTP/2 200 # 状态行(协议版本+状态码)
server: nginx # 服务器类型
date: Tue, 09 Jul 2024 02:00:00 GMT # 响应时间
content-type: text/html; charset=UTF-8 # 内容类型
content-length: 1256 # 内容大小(字节)
last-modified: Mon, 08 Jul 2024 10:00:00 GMT # 最后修改时间
cache-control: max-age=3600 # 缓存控制
etag: "abc123" # 资源标识符
accept-ranges: bytes # 支持范围请求
4. 实用场景示例
(1)检查网站是否存活
curl -I -s -o /dev/null -w "%{http_code}\n" https://example.com
# 输出:200(仅返回状态码)
(2)跟踪重定向链
curl -I -L http://example.com
# 显示所有重定向步骤的头信息
(3)验证CDN缓存命中
curl -I https://cdn.example.com/image.jpg | grep -i "x-cache"
# 输出:X-Cache: HIT 或 X-Cache: MISS
(4)获取文件元数据
curl -I https://example.com/file.zip | grep -E 'Content-Length|Last-Modified'
# 输出文件大小和修改时间
5. 常用组合选项
| 组合选项 | 作用 | 示例 |
|---|---|---|
-I -L |
显示最终响应头(跟随重定向) | curl -IL http://example.com |
-I -v |
显示详细通信过程 | curl -Iv https://api.com |
-I -H "Header: Val" |
添加自定义请求头 | curl -IH "User-Agent: Bot" URL |
-I --compressed |
检查是否支持压缩 | curl -I --compressed URL |
6. 与相关命令对比
| 命令 | 获取内容 | 典型用途 |
|---|---|---|
curl -I |
仅响应头 | 调试HTTP头、检查状态 |
curl -i |
头+Body | 查看完整响应(含头) |
curl -v |
详细通信过程 | 调试底层协议交互 |
curl -s -o /dev/null |
无输出 | 仅测试连接(结合-w提取状态码) |
7. 注意事项
-
协议支持:
-
自动适配 HTTP/1.1、HTTP/2(可通过
--http1.1或--http2强制指定)
-
-
超时控制:
curl -I --connect-timeout 5 https://slow-site.com -
HTTPS安全:
-
默认验证证书,测试时可临时用
-k跳过(不推荐生产使用):curl -Ik https://expired.badssl.com
-
8. 自动化脚本应用
# 检查多个URL状态码
for url in "https://google.com" "https://github.com"; do
status=$(curl -Is "$url" | head -n 1 | cut -d' ' -f2)
echo "$url: $status"
done
输出:
https://google.com: 200 https://github.com: 200
通过 curl -I,您可以高效获取服务器元信息,适用于监控、调试和自动化脚本场景。
郭慕荣博客园

浙公网安备 33010602011771号