curl 是 Linux 系统中非常强大的命令行工具,主要用于传输数据,支持 HTTP、HTTPS、FTP、SFTP 等多种协议。
以下是 curl 最常用的参数分类整理,方便你快速查阅和使用:
基础与常用参数
| 参数 | 说明 | 示例 |
|---|---|---|
-o / --output |
将输出写入指定文件(而不是直接打印到终端) | curl -o page.html https://example.com |
-O / --remote-name |
使用远程文件的文件名保存文件 | curl -O https://example.com/file.zip |
-v / --verbose |
显示详细的调试信息(包括请求头和响应头) | curl -v https://example.com |
-s / --silent |
静默模式,不显示进度条和错误信息 | curl -s https://example.com |
-S / --show-error |
在静默模式下,如果出错则显示错误信息 | curl -sS https://example.com |
-L / --location |
跟随重定向(301/302),自动跳转到新 URL | curl -L https://short.url |
-I / --head |
只显示响应头(HTTP Headers),不显示内容 | curl -I https://example.com |
请求方法与数据发送
| 参数 | 说明 | 示例 |
|---|---|---|
-X / --request |
指定 HTTP 请求方法(GET, POST, PUT, DELETE 等) | curl -X POST https://api.example.com
|
-d / --data |
发送 POST 数据(默认 Content-Type: application/x-www-form-urlencoded) | curl -d "name=test" https://api.example.com |
-d / --data (JSON) |
发送 JSON 数据时,需配合 -H 指定头信息 |
curl -d '{"key":"value"}' -H "Content-Type: application/json" https://api.example.com |
-G / --get |
将 -d 的数据附加到 URL 上作为 GET 请求参数 |
curl -G -d "q=search" https://api.example.com |
-F / --form |
模拟表单提交(multipart/form-data),常用于文件上传 | curl -F "file=@photo.jpg" https://api.example.com/upload |
请求头(Headers)控制
| 参数 | 说明 | 示例 |
|---|---|---|
-H / --header |
自定义请求头 | curl -H "Authorization: Bearer token123" https://api.example.com |
-A / --user-agent |
指定 User-Agent | curl -A "MyApp/1.0" https://example.com |
-e / --referer |
指定 Referer 头(来源页面) | curl -e "https://google.com" https://example.com |
-b / --cookie |
发送 Cookie | curl -b "session_id=abc123" https://example.com |
-c / --cookie-jar |
将服务器返回的 Cookie 保存到文件 | curl -c cookies.txt https://example.com |
-b /
| 从文件读取 Cookie 发送 | curl -b cookies.txt https://example.com |
认证与安全
| 参数 | 说明 | 示例 |
|---|---|---|
-u / --user | 设置基本认证的用户名和密码 | curl -u username:password https://example.com |
--cacert | 指定 CA 证书文件以验证服务器证书 | curl --cacert ca-bundle.crt https://example.com |
-k / --insecure | 跳过 SSL 证书验证(仅用于测试,生产环境慎用) | curl -k https://self-signed.example.com |
--cert | 指定客户端证书文件(mTLS) | curl --cert client.crt --key client.key https://example.com |
进度与超时
| 参数 | 说明 | 示例 |
|---|---|---|
/ --progress-bar | 显示简单的进度条(#号) | curl -# -o file.zip https://example.com/file.zip |
--connect-timeout | 连接超时时间(秒) | curl --connect-timeout 5 https://example.com |
--max-time | 总操作超时时间(秒) | curl --max-time 10 https://example.com |
-r / --range | 只下载文件的一部分(字节范围) | curl -r 0-1024 https://example.com/largefile.zip > part1.bin |
其他实用参数
| 参数 | 说明 | 示例 |
|---|---|---|
--compressed |
请求压缩内容(gzip/deflate),并自动解压 | curl --compressed https://example.com |
-4 / -6 |
强制使用 IPv4 或 IPv6 | curl -4 https://example.com |
--retry |
失败重试次数 | curl --retry 3 https://example.com |
--retry-delay |
重试间隔时间(秒) | curl --retry 3 --retry-delay 2 https://example.com |
🔥 常见使用场景示例
测试 API 接口(GET)
curl -H "Accept: application/json" https://api.example.com/users/1
发送 JSON 数据(POST)
curl -X POST
-H "Content-Type: application/json"
-d '{"name":"John", "age":30}'
https://api.example.com/users
上传文件
curl -X POST -F "file=@/path/to/file.txt" -F "description='My file'" https://api.example.com/upload
查看响应头信息
curl -I https://www.baidu.com
调试网络问题(显示详细交互过程)
curl -v https://example.com
下载文件并重命名
curl -o my_report.pdf https://example.com/reports/annual_report.pdf
💡 小贴士
- 如果命令太长,可以使用
换行,提高可读性。 - 使用
-sS组合可以在静默模式下仍能捕获错误信息,适合脚本中使用。 - 在 Linux 中,
curl通常预装;如果没有,可通过sudo apt install curl(Ubuntu/Debian) 或sudo yum install curl(CentOS/RHEL) 安装。
如需更详细的帮助,请在终端输入:
man curl
或
curl --help
首发原创文章,作者:王坚,如若转载,请注明出处:https://test.idctop.com/article/478594.html




