全部速查表

curl 命令速查

curl 快速参考:HTTP 方法、请求头、认证、JSON、表单、上传下载、重定向、调试、重试与连接控制,并附带常用参数和实操示例。

30 条命令

基础

curl <url>

请求一个 URL 并把响应体输出到 stdout。

-L 跟随重定向;-i 输出包含响应头;-I 仅 HEAD;-v 详细;-s 静默;-S 显示错误;--compressed 解码 gzip/brotli

curl -L -s -o website.html https://example.com && curl -sI https://example.com
curl -O <url> / curl -o <file> <url>

下载文件:-O 保留远端文件名,-o 写入指定路径。

-O;-o file;--create-dirs;--remove-on-error;-C - 断点续传;--retry N;--retry-delay N

curl -O https://nodejs.org/dist/v22.17.0/node-v22.17.0-linux-x64.tar.xz && curl -C - -O https://releases.example.com/sqldump.sql

HTTP 方法与请求头

curl -X <METHOD> <url>

强制指定 HTTP 方法(POST/PUT/PATCH/DELETE 等)。

-X POST/PUT/PATCH/DELETE;--request-method;--request-target

curl -X DELETE -s https://api.example.com/users/42 && curl -X POST https://api.example.com/reset
curl -H 'Header: value' <url>

发送一个或多个自定义请求头(Content-Type、X-API-Key 等)。

-H 'X-API-Key: ...';可重复 -H;--json(同时设置 Content-Type 和 body);--form -F(multipart)

curl -s -H 'Authorization: Bearer $TOKEN' -H 'Accept: application/json' https://api.example.com/orders/1
curl -A <agent> / -e <url>

设置 User-Agent 或 Referer。

-A 'Mozilla/5.0 ...';-e 'https://google.com';--referer 别名

curl -sA 'Mozilla/5.0 (X11; Linux)' -e 'https://google.com' https://example.com/article
curl -i / -D headers.txt / -I <url>

-i 内联输出响应头;-D 将响应头写入文件;-I 仅发送 HEAD。

-i;-D <file>;-I;--dump-header <file>

curl -sI https://api.example.com/health && curl -D headers.txt -o body.json -s https://api.example.com/me

认证

curl -u <user>:<password>

HTTP Basic 认证(密码可省略以交互输入)。

-u user:pass;--basic;-U 指定具体认证方法

curl -u alice:'P@ssw0rd!' -s https://rest.example.com/private/list && curl -u alice -s https://rest.example.com/me
curl -H 'Authorization: Bearer <token>'

发送 Bearer token(也可使用 --oauth2-bearer)。

--oauth2-bearer $TOKEN;--bearer 别名(8+)

curl --oauth2-bearer '$GH_TOKEN' -s https://api.github.com/user | jq .login
curl --cert / --key / --cacert

使用客户端证书 / CA 证书进行 mTLS。

--cert certfile[:password];--key key;--cacert ca.pem;--capath dir

curl --cert client.p12:P@ss --cacert ca.pem -s https://secure.internal/api && curl --cert cert.pem --key key.pem --cacert ca.pem https://...

数据与表单

curl -d '<body>' / -d @file / --data-raw / --data-binary

发送请求体(POST/PUT)。-d 默认按表单编码;--data-binary 保留原始字节。

-d 'a=b&c=d';--data-raw;--data-binary @f;-G 配合 --data-urlencode 可构造查询串

curl -s -X POST -d 'name=alice&[email protected]' https://api.example.com/users && curl --data-binary @body.json -H 'Content-Type: application/json' https://api.example.com/notes
curl -F 'file=@/path'

发送 multipart/form-data 上传(浏览器标准的上传格式)。

-F field=value;-F file=@path;-F '[email protected];type=image/png';--form-string 原始值

curl -s -F 'avatar=@./photo.png' -F 'name=alice' https://api.example.com/profile
curl -G --data-urlencode

构造 URL 编码的查询串(自动对值做 URL 编码)。

-G --data-urlencode 'q=hello world' --data-urlencode 'limit=10'

curl -sG --data-urlencode 'q=hello world' --data-urlencode 'page=2' https://api.example.com/search

JSON

curl -d @body.json -H 'Content-Type: application/json'

在请求体中发送 JSON。

可使用 --json 快捷选项(同时设置 Content-Type 和 body)

curl -s --json @body.json -X POST https://api.example.com/orders && curl --json '{"a":1}' https://api.example.com/x
curl | jq '.'

将响应通过 jq 管道格式化或筛选。

jq '. | {name,id}';jq 'select(.id==42)'

curl -s https://api.github.com/repos/pnpm/pnpm/releases/latest | jq '{tag_name: .tag_name, name: .name, assets: [.assets[].name]}'

Cookie 与会话

curl -b cookies.txt / -c cookies.txt

读取(-b)/ 写入(-c)Cookie。首次调用可加 -j 跟随重定向并捕获。

-c jar.txt 保存;-b "k=v" 内联;-b / -c /tmp/jar 共用同一文件

curl -s -c /tmp/jar.txt -o /dev/null https://app.example.com/login && curl -s -b /tmp/jar.txt https://app.example.com/dashboard
curl --cookie-jar / --cookie

-c / -b 的完整写法,脚本中使用可读性更好。

curl --cookie-jar /tmp/jar --cookie /tmp/jar -s https://app.example.com/me

调试

curl -v / -vv / --trace-ascii

详细输出握手过程;--trace 输出请求 / 响应的原始字节。

-v / -vv / -vvv;--trace-ascii file;--trace file(二进制)

curl -v --trace-ascii /tmp/login.trace https://api.example.com/login
curl --next

在串联的多个请求之间重置参数(避免“URL parm 0”与连接泄漏)。

--next;提供按请求粒度的重置

curl -s -H 'Accept: text/html' https://example.com --next -s -H 'Accept: application/json' https://example.com/api
curl -w '%{http_code}\n'

定义 write-out 模板——在脚本中用于检查耗时或状态码。

-w 'status=%{http_code} time=%{time_total}\n';--write-out

curl -s -o /dev/null -w 'status=%{http_code} time=%{time_total}s bytes=%{size_download}\n' https://example.com/health

传输与重试

curl -C - <url>

从上次中断的位置继续下载。

-C -;--continue-at -

curl -C - -O https://releases.example.com/ubuntu-iso.iso
curl --retry N --retry-delay S --retry-all-errors

自动重试临时性错误(网络、5xx、连接重置)。

--retry;--retry-delay;--retry-max-time;--retry-connrefused;--retry-all-errors(8+)

curl --retry 5 --retry-delay 5 --retry-all-errors --max-time 60 -O https://internal.example.com/big.zip
curl --limit-rate <speed> / --max-time <seconds>

限制带宽或整体运行时长。

--limit-rate 200k;--max-time 30;--connect-timeout 5

curl --limit-rate 1M --max-time 600 -O https://releases.example.com/big.iso
curl -T <file> <url>

通过 PUT 上传文件(配合 -C - 可断点续传)。

-T file;--upload-file;可继续 -C -;可与 -H 'Authorization: ...' 一起使用

curl -T ./dist.tar.gz -H 'Authorization: Bearer $TOKEN' https://uploads.example.com/build/1.4.2.tar.gz

连接

curl --interface <ip> / --dns-servers

绑定指定网卡(如 VPN 接口 IP)或覆盖 DNS,用于拆分 DNS 调试。

--interface eth0:0;--dns-servers 1.1.1.1;--resolve 'host:port:ip' 临时覆盖单次解析

curl --interface 10.99.0.1 -s https://api.internal.example.com && curl --resolve api.internal.example.com:443:127.0.0.1 -s https://api.internal.example.com
curl --http2 / --http3 / --tlsv1.2

协商指定的协议或 TLS 版本。

--http2;--http2-prior-knowledge;--http3(curl ≥ 7.88);--tls-max 1.3;--no-alpn

curl --http3 -sI https://cloudflare-quic.com && curl --tlsv1.2 -s https://api.example.com
curl --insecure / --cacert

跳过或覆盖 TLS 证书校验(--insecure 仅用于调试)。

-k / --insecure;--cacert ca.pem;--capath dir

curl -k --cacert dev-ca.pem -s https://localhost:8443/health

配置

curl -K config.curl

从配置文件中驱动 curl,适合可复现的脚本。

--config file;可使用各种 per-call 选项

curl -sK - <<'EOF'\nurl = 'https://api.example.com/me'\nheader = 'Authorization: Bearer '$'${TOKEN}'\n
curl --parallel / --parallel-max

在同一个 curl 实例中并发请求多个 URL(curl 7.68+)。

--parallel;--parallel-immediate;--parallel-max N

curl --parallel --parallel-max 5 -sO https://example.com/img1.png -sO https://example.com/img2.png

其他

curl --fail-with-body / -f / -fsSL

脚本中常用的“拿 URL”组合:静默、跟随重定向、出错时非零退出、输出。

-f / --fail;--fail-with-body 出错时仍输出 body;-s 静默;-L 跟随重定向;-S 显示错误

curl -fsSL -o installer.sh https://get.docker.com && sh installer.sh
curl --compressed / --no-compressed

请求 gzip / deflate / brotli 压缩响应(--compressed),节省带宽。

--compressed

curl --compressed -s -A 'Mozilla/5.0' https://news.example.com | head -c 200

速查页版本 1.0.0

适用于 curl 7.88+