全部速查表

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 + 內文);--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 預設為 form 編碼;--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 + 內文一次搞定)

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

把回應透過管線送進 jq,方便閱讀/篩選 JSON。

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

讀取 cookie(b)/寫入 cookie(c)。第一次呼叫時加 -j 以同時保存重新導向的 cookie。

-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'

自訂輸出格式——在腳本中檢查耗時或狀態碼很實用。

-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 上傳檔案(或 HTTP PUT/RESUMABLE,搭配 -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——用於 split 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;逐次設定選項

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

腳本中常見的下載慣用法:安靜、跟隨重新導向、HTTP 錯誤時失敗並輸出。

-f / --fail;--fail-with-body 失敗時仍輸出內容;-s 安靜;-L 跟隨;-S 顯示錯誤

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

用 --compressed 要求 gzip、deflate 或 brotli 壓縮傳輸——節省頻寬。

--compressed

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

速查頁版本 1.0.0

適用於 curl 7.88+