curl 速查表
curl 日常用法:发请求、请求头、输出控制、认证、证书、限速,以及请求耗时分析。
基础
curl https://example.com # 简单 GET,把响应体打到标准输出
curl -s https://example.com # 静默:隐藏进度条和报错
curl -sS https://example.com # 静默,但报错照常显示
curl -v https://example.com # 详细:显示请求/响应头、TLS 握手过程
curl -X POST https://example.com # 指定 HTTP 方法(GET/PUT/DELETE…)
curl --http2 https://example.com # 强制 HTTP/2(服务器支持才行)
输出控制
curl -o out.html https://example.com # 把响应体存为 out.html
curl -O https://example.com/file.zip # 用远端文件名保存
curl -D - https://example.com # 把响应头打到标准输出(- 代表 stdout)
curl -i https://example.com # 响应头 + 响应体一起显示
curl -I https://example.com # HEAD 请求:只看响应头,不要 body
curl --compressed https://example.com # 请求 gzip/deflate/br 压缩并自动解压
跳转与重试
curl -L https://short.url/abc # 自动跟随 3xx 跳转
curl -L --max-redirs 5 https://... # 跟随跳转,最多 5 跳
curl --retry 3 https://example.com # 遇到临时性错误最多重试 3 次
curl --retry 3 --retry-delay 2 ... # 重试间隔 2 秒
curl -f https://example.com # 遇到 HTTP 4xx/5xx 直接报错退出(退出码 22)
请求头
curl -H "Authorization: Bearer TOKEN" https://api.example.com # 自定义请求头
curl -H "Accept: application/json" https://api.example.com # 声明想要 JSON
curl -A "MyBot/1.0" https://example.com # 设置 User-Agent
curl -e "https://example.com" https://example.com/page # 设置 Referer
发送数据
curl -d "a=1&b=2" https://example.com/form # POST 表单数据(url-encoded)
curl -d @data.json https://example.com/api # 从文件读取请求体
curl --data-urlencode "q=hello world" https://... # 自动做 URL 编码
curl -X POST https://api.example.com/items \
-H "Content-Type: application/json" \
-d '{"name":"Widget","price":9.99}' # POST 一个 JSON
curl -F "file=@report.pdf" https://example.com/upload # multipart 上传文件
curl -F "file=@a.png" -F "note=hi" https://... # multipart:文件 + 文本字段
认证
curl -u user:pass https://example.com # HTTP Basic 认证
curl -u user https://example.com # 只给用户名,交互式输入密码
curl -H "Authorization: Bearer TOKEN" https://api.example.com # token 放请求头
Cookie
curl -b "session=abc123" https://example.com # 直接带 cookie 字符串
curl -b cookies.txt https://example.com # 从文件读 cookie(Netscape 格式)
curl -c cookies.txt https://example.com/login # 把响应 cookie 存到文件
TLS(证书)
curl -k https://self-signed.badssl.com # 跳过证书校验——仅测试用,正式流量千万别用
curl --cacert ./ca.pem https://internal.corp # 信任自签 CA 证书文件
传输控制
curl --limit-rate 200K https://example.com/big.zip # 限速(K/M/G 后缀)
curl --max-time 30 https://example.com # 整个请求超 30 秒就中断
curl --connect-timeout 5 https://example.com # 建连超 5 秒就中断
curl -C - -O https://example.com/big.zip # 断点续传
测速(找出慢在哪一步)
curl -o /dev/null -s -w "%{time_total}s\n" https://example.com
# 只看总耗时;-o /dev/null 丢弃响应体
curl -o /dev/null -s -w "dns:%{time_namelookup} tcp:%{time_connect} tls:%{time_appconnect} ttfb:%{time_starttransfer} total:%{time_total}\n" https://example.com
# 全阶段耗时拆解(各值是累计秒数,相减才是各阶段耗时)
curl -o /dev/null -s -w "@timing.txt" https://example.com
# 从文件读取 -w 格式(@- 表示从 stdin 读)
curl -o /dev/null -s -w "%{http_code} %{size_download} %{speed_download}\n" https://example.com
# 状态码、下载字节数、平均速度
代理
curl -x http://127.0.0.1:7890 https://example.com # 走代理
curl -U user:pass -x http://proxy:8080 https://... # 带认证的代理