一、curl 是什么
curl(Client URL)是一个功能强大的命令行网络工具,支持 HTTP、HTTPS、FTP、SFTP、SMTP 等多种协议。它被广泛应用于 API 调试、文件传输、网络诊断等场景,是 Linux 运维和开发人员必备的工具之一。
Daniel Stenberg 于 1997 年创建了 curl,如今它已成为全球最广泛使用的网络工具——几乎每个 Linux 发行版都预装了 curl,Windows 10/11 也内置了它。
二、安装与基本使用
2.1 安装
大多数 Linux 发行版默认已安装 curl,如需手动安装:
1 2 3 4 5 6 7 8 9 10 11
| sudo apt install curl -y
sudo dnf install curl -y
apk add curl
brew install curl
|
验证安装:
2.2 最简单的用法
1 2 3 4 5 6 7 8
| curl https://example.com
curl -I https://example.com
curl -s https://api.github.com
|
三、常用选项详解
以下是 curl 最常用的选项速查表:
| 选项 | 长选项 | 说明 |
-o | --output | 将输出写入文件 |
-O | --remote-name | 用远程文件名保存 |
-L | --location | 跟随重定向 |
-i | --include | 输出中包含响应头 |
-I | --head | 仅获取响应头 |
-k | --insecure | 允许不安全的 SSL 连接 |
-u | --user | HTTP 基本认证用户名:密码 |
-v | --verbose | 显示详细通信过程 |
-s | --silent | 静默模式 |
-S | --show-error | 静默模式下仍显示错误 |
-w | --write-out | 请求完成后输出额外信息 |
-x | --proxy | 使用代理 |
-C | --continue-at | 断点续传 |
-m | --max-time | 最大请求时间(秒) |
--connect-timeout | — | 连接超时时间(秒) |
--retry | — | 失败重试次数 |
--limit-rate | — | 限速(字节/秒) |
四、HTTP 请求定制
4.1 指定请求方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| curl https://api.example.com/users
curl -X POST https://api.example.com/users
curl -X PUT https://api.example.com/users/1
curl -X DELETE https://api.example.com/users/1
curl -X PATCH https://api.example.com/users/1
|
4.2 发送请求体
1 2 3 4 5 6 7 8 9 10 11
| curl -X POST -d "name=Alice&email=alice@example.com" https://api.example.com/users
curl -X POST \ -H "Content-Type: application/json" \ -d '{"name":"Alice","email":"alice@example.com"}' \ https://api.example.com/users
curl -X POST -H "Content-Type: application/json" -d @data.json https://api.example.com/users
|
4.3 自定义请求头
1 2 3 4 5 6 7 8
| curl -H "Authorization: Bearer token123" https://api.example.com/protected
curl -H "Authorization: Bearer token123" \ -H "Accept: application/json" \ -H "User-Agent: MyApp/1.0" \ https://api.example.com/protected
|
4.4 Cookie 管理
1 2 3 4 5 6 7 8
| curl -b "session_id=abc123; user=alice" https://example.com/profile
curl -b cookies.txt https://example.com/profile
curl -c cookies.txt https://example.com/login -d "username=admin&password=secret"
|
五、文件下载与上传
5.1 下载文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| curl -o myfile.zip https://example.com/files/large.zip
curl -O https://example.com/files/large.zip
curl -C - -o myfile.zip https://example.com/files/large.zip
curl --limit-rate 100K -O https://example.com/files/large.zip
curl -# -O https://example.com/files/large.zip
|
5.2 上传文件
1 2 3 4 5 6 7 8
| curl -F "file=@/path/to/photo.jpg" https://example.com/upload
curl -F "avatar=@photo.jpg;type=image/jpeg" https://example.com/upload
curl -T localfile.txt ftp://ftp.example.com/upload/
|
六、认证与 HTTPS
6.1 HTTP 基本认证
1 2 3 4 5 6 7 8 9
| curl -u username:password https://api.example.com/protected
curl https://username:password@api.example.com/protected
curl -n https://api.example.com/protected
|
6.2 Bearer Token 认证
1 2
| curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." https://api.example.com/protected
|
6.3 SSL/TLS 相关
1 2 3 4 5 6 7 8 9 10 11
| curl -k https://self-signed.example.com
curl --cert client.crt --key client.key https://api.example.com
curl --cacert /path/to/ca-bundle.crt https://api.example.com
curl -v https://example.com 2>&1 | grep -E "SSL|TLS|certificate"
|
七、API 调试实战
7.1 REST API 测试场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| curl -s https://jsonplaceholder.typicode.com/users | head -20
curl -s -X POST \ -H "Content-Type: application/json" \ -d '{"name":"Alice","username":"alice","email":"alice@example.com"}' \ https://jsonplaceholder.typicode.com/users
curl -s -X PUT \ -H "Content-Type: application/json" \ -d '{"name":"Alice Updated"}' \ https://jsonplaceholder.typicode.com/users/1
curl -s -X PATCH \ -H "Content-Type: application/json" \ -d '{"email":"newemail@example.com"}' \ https://jsonplaceholder.typicode.com/users/1
curl -s -X DELETE https://jsonplaceholder.typicode.com/users/1
|
7.2 响应分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| curl -v https://api.github.com
curl -s -D - https://api.github.com -o /dev/null
curl -s -w "\n\n========== 时间统计 ==========\n\ DNS 解析: %{time_namelookup}s\n\ TCP 连接: %{time_connect}s\n\ TLS 握手: %{time_appconnect}s\n\ 首字节: %{time_starttransfer}s\n\ 总耗时: %{time_total}s\n\ 下载速度: %{speed_download} bytes/s\n\ HTTP 状态码: %{http_code}\n" \ https://api.github.com -o /dev/null
|
7.3 配合 jq 处理 JSON 响应
1 2 3 4 5 6 7 8 9 10 11
| sudo apt install jq -y
curl -s https://api.github.com/repos/curl/curl | jq '.stargazers_count'
curl -s https://jsonplaceholder.typicode.com/users | jq '.[] | {name, email, phone}'
curl -s https://jsonplaceholder.typicode.com/users | jq '.[] | select(.name | contains("Lea"))'
|
八、进阶用法
8.1 多请求并发
1 2 3 4 5 6
| sudo apt install parallel -y
echo -e "https://api.github.com\nhttps://httpbin.org/get\nhttps://jsonplaceholder.typicode.com/todos/1" | \ parallel -j 3 "curl -s -o /dev/null -w '%{http_code} %{url_effective}\n' {}"
|
8.2 使用代理
1 2 3 4 5 6 7 8
| curl -x http://proxy.example.com:8080 https://httpbin.org/get
curl --socks5 localhost:1080 https://httpbin.org/get
curl -x http://user:pass@proxy.example.com:8080 https://httpbin.org/get
|
8.3 使用配置文件
curl 支持从配置文件读取选项,避免每次输入冗长的参数:
1 2 3 4 5 6 7 8 9 10 11
| cat > ~/.curlrc << 'EOF' --connect-timeout 10 --max-time 30 --retry 3 --retry-delay 2 --user-agent "MyCurl/1.0" EOF
curl https://example.com
|
8.4 发送邮件(SMTP)
1 2 3 4 5
| curl --mail-from sender@example.com \ --mail-rcpt recipient@example.com \ --upload-file mail.txt \ smtp://smtp.example.com:587 \ --user sender@example.com:password
|
九、常见问题排查
| 问题 | 可能原因 | 解决方案 |
curl: (6) Could not resolve host | DNS 解析失败 | 检查网络连接和 DNS 配置(/etc/resolv.conf) |
curl: (7) Failed to connect | 目标服务器不可达 | 检查端口是否开放:nc -zv host port |
curl: (28) Connection timed out | 连接超时 | 增加 --connect-timeout 或检查防火墙 |
curl: (35) SSL connection error | TLS/SSL 握手失败 | 使用 -v 查看详情,检查证书有效性 |
curl: (56) Recv failure | 连接被重置 | 可能是防火墙拦截或服务端异常关闭 |
curl: (60) SSL certificate problem | 证书无效或自签名 | 测试时用 -k,生产需安装正确证书 |
curl: (92) HTTP/2 stream 0 was not closed cleanly | HTTP/2 协议错误 | 尝试 --http1.1 降级到 HTTP/1.1 |
十、安全最佳实践
- 避免在命令行直接输入密码——使用
~/.netrc 文件或环境变量
- 生产环境不要使用
-k 跳过证书验证——这会暴露于 MITM 攻击
- 传输敏感数据必须使用 HTTPS——curl 默认支持,但注意
-k 会降低安全性
- 日志中过滤敏感信息——不要在日志中记录
Authorization 头的 token 值
- 合理设置超时和重试——防止脚本因网络问题卡死
- 注意文件权限——保存 API 令牌或 Cookie 的文件应设置
600 权限
十一、命令速查表
| 场景 | 命令 |
| 简单请求 | curl https://example.com |
| POST JSON | curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL |
| 带 Token | curl -H "Authorization: Bearer TOKEN" URL |
| 下载文件 | curl -O URL |
| 上传文件 | curl -F "file=@/path" URL |
| 查看响应头 | curl -I URL |
| 调试输出 | curl -v URL |
| 性能测量 | curl -w "时间统计..." URL |
| 断点续传 | curl -C - -o file URL |
| 代理请求 | curl -x http://proxy:port URL |
| 跟随重定向 | curl -L URL |
| 带 Cookie | curl -b "key=value" URL |
总结
curl 虽然只是一个命令行工具,但它的功能极其丰富。无论是日常的 API 调试、文件传输,还是深入的网络故障排查,curl 都能胜任。掌握 curl 的核心用法不仅能提高工作效率,还能帮你更好地理解 HTTP 协议和网络通信机制。建议把本文的速查表保存下来,在实际工作中多练习方能熟练运用。
本文由AI辅助生成,内容仅供参考