技術文章 · IT 指令工具箱

curl 指令大全:測網址、下載檔案、API、Header 與 TLS

curl 怎麼用?整理查看 HTTP 狀態、Header、重新導向、下載、上傳、JSON API、驗證、逾時、代理與 TLS 憑證排查的常用指令。

作者 Steve Chen · 發布  · 約 7 分鐘閱讀

IT 指令工具箱 — 廷皓技術專欄插圖

curl 很適合把「瀏覽器打不開」拆成 DNS、TCP、TLS、HTTP 與應用程式幾層。先從唯讀 GET/HEAD 開始,再處理會送資料或改狀態的 API。

24 組範例LinuxmacOSWindows curl查核日期:2026-08-11
動手前:網址、Header 與回應可能含 token、cookie、內網名稱或個資。不要把 -v 完整輸出公開;也不要用 -k/--insecure 當永久解法。

查看網站與連線

只看回應 Headercurl

curl -I https://example.com/

使用 HEAD;有些應用不支援 HEAD,結果不一定等同 GET。

顯示 Header 與 Bodycurl

curl -i https://example.com/

適合看狀態碼、cookie 與回應內容。

跟隨重新導向curl

curl -L https://example.com/old

-L 會跟進 Location;跨網域時留意認證資訊。

只輸出狀態碼curl

curl -sS -o /dev/null -w '%{http_code}
' https://example.com/health

適合健康檢查,但仍要判斷應用內容。

輸出完整計時curl

curl -sS -o /dev/null -w 'dns=%{time_namelookup} connect=%{time_connect} tls=%{time_appconnect} first=%{time_starttransfer} total=%{time_total}
' https://example.com/

可把延遲拆成 DNS、連線、TLS 與首 byte。

顯示詳細連線過程curl

curl -v https://example.com/

可能顯示敏感 Header,分享前遮蔽。

限制總逾時curl

curl --connect-timeout 5 --max-time 20 https://example.com/

connect timeout 與整體 max time 是兩個門檻。

指定解析結果測試curl

curl --resolve example.com:443:203.0.113.10 https://example.com/

保留正確 Host 與 SNI,只暫時繞過 DNS;IP 換成實際測試端。

強制 IPv4curl

curl -4 https://example.com/

可對照 IPv6 路徑是否有問題。

強制 IPv6curl

curl -6 https://example.com/

本機、DNS 與路由都必須有 IPv6。

下載與上傳

下載並沿用遠端檔名curl

curl -LO https://example.com/files/manual.pdf

-O 使用網址最後一段當檔名,先確認所在目錄。

指定下載檔名curl

curl -fL -o manual.pdf https://example.com/download

-f 讓 HTTP 4xx/5xx 回失敗,-L 跟重新導向。

中斷後續傳curl

curl -C - -LO https://example.com/files/large.iso

伺服器需支援 Range;完成後比對 checksum。

限制下載速度curl

curl --limit-rate 2M -LO https://example.com/files/large.iso

降低對共用線路影響。

上傳單一檔案curl

curl -fS -T report.csv https://upload.example.com/inbox/report.csv

通常還需要認證;確認 PUT 不會覆蓋重要檔案。

multipart 表單上傳curl

curl -fS -F '[email protected]' https://api.example.com/upload

@ 代表讀取本機檔案;先確認 API 文件。

呼叫 API

送出 JSON POSTcurl

curl -fS -X POST https://api.example.com/v1/jobs -H 'Content-Type: application/json' --data '{"name":"nightly"}'

這可能建立資料;先在測試環境確認 endpoint。

從檔案送 JSONcurl

curl -fS https://api.example.com/v1/jobs -H 'Content-Type: application/json' --data-binary @payload.json

避免複雜 JSON 卡在 shell 引號。

Bearer token 用環境變數curl

curl -fS https://api.example.com/v1/me -H "Authorization: Bearer $API_TOKEN"

環境變數仍可能被子程序讀取;不要把實值寫進文章、script 或 history。

Basic Auth 互動輸入密碼curl

curl -u apiuser https://api.example.com/v1/me

只寫帳號,curl 會提示輸入密碼,避免直接出現在 history。

保存 cookiecurl

curl -c cookies.txt https://example.com/login

cookie 檔可能等同登入憑證,權限應限制並用完移除。

帶入 cookiecurl

curl -b cookies.txt https://example.com/account

不要分享 cookies.txt。

透過 HTTP proxycurl

curl -x http://proxy.example:8080 https://example.com/

若 proxy 需要帳密,避免直接寫在命令列。

顯示伺服器憑證錯誤但不繞過curl

curl -v https://expired.example.com/

保留驗證失敗才看得到真正問題;不要加 -k。

怎麼確認有做對

  • 檢查 curl 結束碼與 HTTP status。
  • API 寫入後用獨立 GET 查詢,不只相信 200。
  • 下載檔案用官方 SHA-256 或簽章驗證。

常見錯誤

  • 把 -k/--insecure 當正式設定。
  • 只有 HTTP 200 就認定內容正確。
  • token、cookie、Basic Auth 密碼出現在 shell history 或工單。
  • 未確認 API 語意便用 POST、PUT、PATCH、DELETE。

版本與官方文件

參數會隨工具版本與作業系統實作改變。正式環境先用 --help、-h 或系統內建說明確認,再以當版官方文件為準。

聯絡廷皓討論 看更多文章