curl [options] [URL...]
| 短參數 | 長參數 | 說明 |
|---|---|---|
| -o | --output | 輸出到指定檔案 |
| -O | --remote-name | 用遠端檔名存檔 |
| -C - | --continue-at - | 斷點續傳 |
| -L | --location | 跟隨重導向 |
| -H | --header | 自定義請求頭 |
| -A | --user-agent | 設定 User-Agent |
| -b | --cookie | 發送 Cookie |
| -c | --cookie-jar | 儲存 Cookie 到檔案 |
| -D | --dump-header | 儲存回應頭到檔案 |
| -X | --request | 指定 HTTP 方法 (GET/POST/PUT/DELETE) |
| -d | --data | POST 資料 (application/x-www-form-urlencoded) |
| -F | --form | multipart/form-data (檔案上傳) |
| -u | --user | Basic Auth 帳號:密碼 |
| -x | --proxy | 代理伺服器 [protocol://]host:port |
| -k | --insecure | 跳過 TLS 憑證驗證 |
| -s | --silent | 靜默模式 (不顯示進度/錯誤) |
| -S | --show-error | 配合 -s 顯示錯誤 |
| -w | --write-out | 自定義輸出格式 (效能分析神器) |
| -# | --progress-bar | 進度條顯示 |
| -I | --head | 僅獲取 HEAD 回應 |
| -v | --verbose | 詳細溝通過程 |
# 直接輸出到終端
curl https://example.com
# 存檔 (兩種方式)
curl -o page.html https://example.com
curl -O https://example.com/file.zip
curl -C - -O https://example.com/large.iso
curl -o /dev/null -s -w "%{http_code}\n" https://example.com
# HTTP 代理
curl -x 192.168.1.100:3128 https://example.com
# 偽裝 User-Agent
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://example.com
# 偽造 Referer (防盜鏈)
curl -e "https://example.com/page.html" https://example.com/image.jpg
# 登入並儲存 Cookie
curl -c cookies.txt -d "user=foo&pass=bar" https://example.com/login
# 後續請求攜帶 Cookie
curl -b cookies.txt https://example.com/profile
curl -F "file=@/path/to/local.png" -F "name=avatar" https://example.com/upload
# 下載
curl -O -u user:pass ftp://ftp.example.com/file.txt
# 上傳
curl -T local.txt -u user:pass ftp://ftp.example.com/remote.txt
curl -O "https://example.com/img[1-10].jpg"
curl -O "https://example.com/{a,b,c}/file.txt"
curl -r 0-10485759 -o part1 https://example.com/big.iso
curl -r 10485760-20971519 -o part2 https://example.com/big.iso
curl -r 20971520- -o part3 https://example.com/big.iso
cat part* > big.iso
# 僅顯示下載速度
curl -Lo /dev/null -skw "%{speed_download}\n" https://mirrors.163.com/ubuntu/ls-lR.gz
# 完整時間分解
curl -Lo /dev/null -skw "
time_connect: %{time_connect} s
time_namelookup: %{time_namelookup} s
time_pretransfer: %{time_pretransfer} s
time_starttransfer: %{time_starttransfer} s
time_redirect: %{time_redirect} s
speed_download: %{speed_download} B/s
time_total: %{time_total} s
" https://www.sina.com
輸出範例:
time_connect: 0.154 s
time_namelookup: 0.150 s
time_pretransfer: 0.154 s
time_starttransfer: 0.163 s
time_redirect: 0.157 s
speed_download: 324679.000 B/s
time_total: 1.692 s
#!/bin/bash
API="https://api.example.com/v1"
TOKEN="Bearer xxx"
curl -s -X POST "$API/orders" \
-H "Authorization: $TOKEN" \
-H "Content-Type: application/json" \
-d '{"item_id":123,"qty":2}' \
-w "\nHTTP %{http_code} | Total: %{time_total}s | Size: %{size_download}B\n" \
-o response.json
cat response.json | jq .
-d 會自動加 Content-Type: application/x-www-form-urlencoded;送 JSON 需手動 -H "Content-Type: application/json"。-F 會自動設 multipart/form-data 並產生 boundary。-L。-k,生產環境請改用 --cacert 指定 CA。--limit-rate 避免佔滿頻寬。