TL;DR
curl 是调试 HTTP/API 的第一工具。最常用:curl -sS URL、curl -X POST -d '{}' -H 'Content-Type: application/json' URL、curl -w '%{http_code}'。
基础请求
| 需求 | 命令 |
|---|
| GET 并显示响应头 | curl -i URL |
| 静默模式(不显示进度) | curl -sS URL |
| 只看响应头 | curl -I URL |
| 跟随重定向 | curl -L URL |
| 输出到文件 | curl -o file.txt URL / -O(按远程文件名) |
带数据请求
| 需求 | 命令 |
|---|
| POST JSON | curl -X POST -H 'Content-Type: application/json' -d '{"k":"v"}' URL |
| 表单提交 | curl -d 'k=v&k2=v2' URL |
| 上传文件 | curl -F 'file=@./a.txt' URL |
| 自定义请求头 | curl -H 'Authorization: Bearer TOKEN' URL |
| Cookie | curl -b 'name=value' URL / -c cookies.txt 保存 |
实用参数
| 参数 | 作用 |
|---|
-w '%{http_code}\n' | 只输出状态码 |
--max-time 10 | 超时(秒) |
--connect-timeout 5 | 连接超时 |
-k / --insecure | 跳过 TLS 校验(仅调试) |
-x http://host:port | 走代理 |
--limit-rate 1M | 限速 |
-A 'UA' | 自定义 User-Agent |
-u user:pass | 基础认证 |
高频排查
- 返回 000 = 连接失败/超时,先试
--max-time 和 -v; -v 查看握手与请求头细节;curl -s -o /dev/null -w '%{http_code}' 是脚本里检查状态码的标准写法。
来源
- curl 官方文档(curl.se/docs/manpage.html)
最后更新:2026-08-02