後知後覺新發現 - 原來 Windows 已內建 curl (附常用語法範例)
| | 1 | | 2,150 |
分享我後知後覺的新發現 - Windows 10/11 有內建 curl... 很久了~
在新裝的 Windows 主機查問題,同事給我一段 curl 語法請我測網頁存取是否正常。我在心中噗哧一笑,想說這又不是 Linux,新主機也沒裝 Cmder 或 Git,哪來 curl 可用? 但順手一試,靠! 真有耶~ 幸好沒表現出來,沒人發現我孤陋寡聞。(噗,這下子全天下都知道了)
在 Windows 下 curl -V
由版本宣告中的 "(Windows)" 可知這是由微軟維護的版本,與 curl 官方有些差異,
依據 curl 官網說明,curl 是從 2017/12/19 開始成為 Winodws 內建工具,目前的 Windows 版 curl 少了一些功能,如 Public Suffix List (PSL 支援,可防止跨站台共享的 Super Cookies)、HTTP/2、HTTP/3、brotli/zstd 壓縮,且不支援 GOPHER/LDAP/RTMP/RTSP/SCP/SFTP/WS 等協定。但應付一般網站或 WebAPI 檢測及操作綽綽有餘。
與 PowerShell Invoke-WebRequest 相比,curl CLI 更簡單扼要,若是簡單的 GET/POST 或下載請求,不需要複雜的前置參數處理及回應解析,改用 curl 更簡潔,而熟悉 curl 技巧可通吃 Windows/Linux/macOS,也是改用它的一大優勢。
(註:PowerShell 命令提示視窗的 curl 是 Invoke-WebRequest 的別名,若要呼叫 curl CLI,可改輸入 curl.exe)
趁著這篇,整理我常用的 curl 語法備忘,省得每次要到處查。
- 一般 GET 呼叫
# 顯示 HTML 內容 curl https://www.example.com # 將結果寫入檔案 curl https://www.example.com > save.txt # 只顯示 Response Header 不下載內容 curl -I https://www.example.com # 依(301/302)回應自動導向新網址 curl -L http://www.example.com
- 下載檔案
# 下載儲存至指定檔名 curl -o my-filename.zip https://www.example.com/download.zip # 下載儲存為網站指定檔名 curl -O https://www.example.com/download.zip
- 發送 POST 請求
# Form 欄位 curl -X POST -d "a=1&b=2" https://www.example.com/form # 以 JSON 傳送參數 curl -X POST -H "Content-Type: application/json" -d '{"a":"1"}' https://www.example.com/api
- 顯示傳輸效能資訊 (延伸閱讀:在 CLI 簡易測量網頁回應時間)
# -w 指定輸出樣版、-o NUL (Linux/macOS 為 -o /dev/null) 將回傳結果導向黑洞 curl -w " dns: %{time_namelookup} tcp: %{time_connect} tls: %{time_appconnect} redir: %{time_redirect}\nproc: %{time_pretransfer} ttfb: %{time_starttransfer} totl: %{time_total} size: %{size_download}" -o NUL -s https://www.google.com # 或將輸出樣版存取檔案 echo dns: %{time_namelookup} tcp: %{time_connect} tls: %{time_appconnect} redir: %{time_redirect}\nproc: %{time_pretransfer} ttfb: %{time_starttransfer} totl: %{time_total} size: %{size_download} > tmpl.txt curl -w "@tmpl.txt" -o NUL -s https://www.google.com
- 其他常用參數
# 忽略 TLS 憑證檢查 curl -k https://some.self-signed.web # 靜默模式,不顯示進度條或錯誤訊息 curl -s https://www.example.com > output.html # 要忽略傳回結果可加上 -o NUL (相當於 Linux /dev/null) curl -I https://www.example.com -o NUL
- 保存 Cookie
# 使用 -c 指定檔案記錄 Cookie curl -c cookies.txt -s http://localhost/AspNet/CookieTest/TestCookie.aspx # Cookie 記錄的文字檔,可直接檢視或修改內容 type cookies.txt # 使用 -b 附上 Cookie curl -b cookies.txt -s http://localhost/AspNet/CookieTest/TestCookie.aspx # 使用 -b "KEY=VALUE" 可指定 Cookie 值 curl -b "Demo=BY_PARAM" -s http://localhost/AspNet/CookieTest/TestCookie.aspx # 同時使用 -b/-c 讀寫 Cookie curl -b cookies.txt -c cookies.txt -s http://localhost/AspNet/CookieTest/TestCookie.aspx
上述測試伺服器端 TestCookie.aspx 程式如下:<%@Page Language="C#"%> <script runat="server"> void Page_Load(object sender, EventArgs e) { var cookie = Request.Cookies["Demo"]; if (cookie == null) { var cookieVal = DateTime.Now.ToString("mmssfff"); Response.Write("Cookie created: " + cookieVal); Response.Cookies.Add(new HttpCookie("Demo") { Value = cookieVal }); } else Response.Write("Cookie read: " + cookie.Value); } </script>
最後,為防有同學不知道,在 Chrome/Edge 的 F12 網路檢視的每筆請求上按右鍵,你可以選擇複製成 cURL 指令,貼到 Cmd 用 CLI 重現網路動作,查問題時格外好用。
Windows 10/11 has built-in curl maintained by Microsoft. While it lacks some features compared to the official version, it is sufficient for most web and API tasks. Here’s a quick reference for common curl commands on Windows, useful for GET/POST requests, file downloads, and cookie handling.
Comments
# by Chan
可以用 scoop 安裝 curl 來用用看 ``` curl 8.12.1 (x86_64-w64-mingw32) libcurl/8.12.1 LibreSSL/4.0.0 zlib/1.3.1 brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.65.0 ngtcp2/1.11.0 nghttp3/1.8.0 Release-Date: 2025-02-13 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd ```