萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux curl 指令完整參數詳解:HTTP 請求、代理、Cookie、認證、檔案上傳與標頭控制

curl 核心定位

curl (Client URL) 是命令列下的資料傳輸瑞士刀,支援 HTTP/HTTPS/FTP/FTPS/SFTP/TFTP/SMTP/POP3/IMAP/LDAP/MQTT 等 20+ 協議。核心優勢:腳本自動化、除錯 HTTP 交互、API 測試、檔案下載上傳。

基礎請求

# GET 請求 (預設)
curl https://api.example.com/users

# 顯示回應標頭
curl -I https://api.example.com/users
curl -i https://api.example.com/users  # 標頭 + 主體

# 跟隨重導向
curl -L https://short.url/redirect

# 指定 HTTP 方法
curl -X POST https://api.example.com/users
curl -X PUT https://api.example.com/users/123
curl -X DELETE https://api.example.com/users/123

資料傳送

GET 參數

curl -G -d "name=value&name2=value2" https://api.example.com/search
# 實際 URL: https://api.example.com/search?name=value&name2=value2

POST 表單資料 (application/x-www-form-urlencoded)

curl -d "name=value&name2=value2" https://api.example.com/users
# 或分行更易讀
curl -d "name=value" -d "name2=value2" https://api.example.com/users

POST JSON (API 常用)

curl -H "Content-Type: application/json" \
  -d '{"name":"John","age":30}' \
  https://api.example.com/users

檔案上傳 (multipart/form-data)

# 單檔
curl -F "file=@/path/to/file.pdf" https://api.example.com/upload

# 多檔 + 額外欄位
curl -F "file=@/path/to/file.pdf" -F "filename=report.pdf" -F "user_id=123" \
  https://api.example.com/upload

# 指定 MIME 類型
curl -F "file=@/path/to/file.pdf;type=application/pdf" https://api.example.com/upload

標頭控制

# 設定請求標頭
curl -H "Authorization: Bearer TOKEN" \
  -H "Accept: application/json" \
  -H "User-Agent: MyApp/1.0" \
  https://api.example.com/users

# 設定 Referer
curl -e "https://referrer.example.com" https://api.example.com/users

# 設定 User-Agent 簡寫
curl -A "MyApp/1.0" https://api.example.com/users

# 儲存回應標頭至檔案
curl -D /tmp/headers.txt https://api.example.com/users

Cookie 管理

# 接收並儲存 Cookie (Cookie Jar)
curl -c /tmp/cookies.txt https://api.example.com/login -d "user=admin&pass=secret"

# 發送儲存的 Cookie
curl -b /tmp/cookies.txt https://api.example.com/profile

# 直接指定 Cookie 字串
curl -b "session=abc123; token=xyz789" https://api.example.com/profile

代理與 TLS

# HTTP/HTTPS 代理
curl -x https://proxy.example.com:3128 https://api.example.com
curl -x https://user:pass@proxy.example.com:3128 https://api.example.com

# SOCKS4/5 代理
curl --socks4 127.0.0.1:1080 https://api.example.com
curl --socks5 127.0.0.1:1080 https://api.example.com
curl --socks5-hostname 127.0.0.1:1080 https://api.example.com  # 遠端 DNS 解析

# 忽略 TLS 憑證驗證 (測試環境)
curl -k https://self-signed.example.com
# 或指定 CA 憑證
curl --cacert /path/ca.pem https://api.example.com

# 客戶端憑證認證 (mTLS)
curl --cert /path/client.pem --key /path/client.key https://api.example.com

認證

# Basic Auth
curl -u username:password https://api.example.com
# 等同 -H "Authorization: Basic base64(username:password)"

# Bearer Token
curl -H "Authorization: Bearer eyJhbGciOi..." https://api.example.com

輸出控制與進度

# 儲存回應主體至檔案
curl -o /tmp/response.json https://api.example.com/users
# 以遠端檔名儲存 (-O)
curl -O https://example.com/file.zip

# 靜默模式 (無進度條/錯誤訊息)
curl -s https://api.example.com/users
# 靜默但顯示錯誤
curl -sS https://api.example.com/users

# 進度條樣式
curl -# https://example.com/large.file  # # 號進度條
curl --progress-bar https://example.com/large.file  # 等同

# 限制速度
curl --limit-rate 1m https://example.com/large.file  # 1 MB/s

# 斷點續傳
curl -C - -O https://example.com/large.file

除錯與追蹤

# 詳細輸出 (含握手、標頭、資料流向)
curl -v https://api.example.com/users

# 只輸出溝通過程不顯示主體
curl -v -o /dev/null https://api.example.com/users

# 追蹤重導向鏈
curl -v -L https://short.url/redirect 2>&1 | grep -E '(^\*|^< HTTP|^> Host|^< Location)'

# 格式化輸出關鍵指標
curl -w '\nDNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n' -o /dev/null -s https://api.example.com

實戰範例集錦

# 1. 健康檢查腳本
curl -sf -o /dev/null -w "%{http_code}" https://api.example.com/health || exit 1

# 2. 下載並驗證 SHA256
curl -sSL https://example.com/app.tar.gz | tee app.tar.gz | sha256sum -c <(echo "expected_hash  -")

# 3. 批次 API 呼叫 (配合 jq)
for id in {1..100}; do
  curl -s "https://api.example.com/items/$id" | jq .name
done

# 4. 測試 POST 表單上傳
curl -v -F "file=@test.txt" https://httpbin.org/post

# 5. 模擬瀏覽器完整請求
curl -v -A "Mozilla/5.0" -e "https://google.com" -b cookies.txt -c cookies.txt \
  -H "Accept-Language: zh-TW,zh;q=0.9" \
  https://example.com

常見陷阱

  • URL 未加引號:含 &、?、# 等特殊字元時必須用雙引號包裹。
  • -d 會自動設定 Content-Type: application/x-www-form-urlencoded,傳 JSON 時必須手動覆蓋 -H "Content-Type: application/json"。
  • -F 強制 multipart/form-data,不可與 -d 混用。
  • -k 僅供測試,**生產環境嚴禁**忽略憑證驗證。

結語

curl 參數極豐富,核心掌握 -X 方法、-H 標頭、-d/-F 資料、-b/-c Cookie、-x 代理、-u 認證、-o/-O 輸出、-v 除錯、-w 格式化 九大類即可應對 95% 場景。配合 jq 處理 JSON、httpie 友善介面、postman 圖形化測試,構建完整 API 開發測試工具鏈。

copyright © 萬盛學電腦網 all rights reserved