10 cURL 命令用法與實時示例

已發表: 2015-02-12

cURL 是一個使用 URL 語法獲取或發送數據的命令行工具。

如果您是開發人員或支持職能部門,則必須了解cURL 命令的用法以對 Web 應用程序進行故障排除。 cURL 是一個跨平台實用程序,意味著您可以在 Windows、MAC 和 UNIX 上使用。

以下是一些最常用的語法,並帶有一個示例來幫助您。

驗證是否可以連接到 URL

如果您在 UNIX 系統上工作並嘗試連接外部 URL,那麼您要做的第一件事就是檢查是否可以通過 curl 訪問該 URL。

curl yoururl.com

它不會拋出任何輸出。 但是,如果服務器無法連接,則會出現無法解析主機等錯誤。

 [root@gf-lab tmp]# curl helloitdoesntexist.com curl: (6) Could not resolve host: helloitdoesntexist.com; Unknown error [root@gf-lab tmp]#

將 URL/URI 輸出保存到文件

如果必須將 URL 或 URI 內容保存到特定文件,可以使用以下語法

curl https://yoururl.com > yoururl.html

前任:

 [root@gf-lab tmp]# curl https://gf.dev > /tmp/gfhtml % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 18557 0 18557 0 0 72565 0 --:--:-- --:--:-- --:--:-- 72772 [root@gf-lab tmp]#

上面的例子會將 gf.dev 中的所有內容保存到 /tmp/gf.html

顯示請求和響應標頭

如果您遇到問題並想要驗證,您將獲得預期的請求和響應標頭。

 curl -v yoururl.com

前任:

 [root@gf-lab tmp]# curl -v https://geekflare.com * About to connect() to geekflare.com port 443 (#0) * Trying 104.25.134.107... * Connected to geekflare.com (104.25.134.107) port 443 (#0) * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 * Server certificate: * subject: CN=ssl371609.cloudflaressl.com,OU=PositiveSSL Multi-Domain,OU=Domain Control Validated * start date: Nov 07 00:00:00 2019 GMT * expire date: May 15 23:59:59 2020 GMT * common name: ssl371609.cloudflaressl.com * issuer: CN=COMODO ECC Domain Validation Secure Server CA 2,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: geekflare.com > Accept: */* > < HTTP/1.1 200 OK < Date: Sat, 09 Nov 2019 19:41:37 GMT < Content-Type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < Set-Cookie: __cfduid=d2ce6cd359ebc0b6eb5ff3a454ed042021573328497; expires=Sun, 08-Nov-20 19:41:37 GMT; path=/; domain=.geekflare.com; HttpOnly; Secure < Vary: Accept-Encoding < Link: <https://geekflare.com/wp-json/>; rel="https://api.w.org/" < Link: <https://geekflare.com/>; rel=shortlink < X-SRCache-Fetch-Status: HIT < X-SRCache-Store-Status: BYPASS < X-Frame-Options: SAMEORIGIN < X-Powered-By: EasyEngine v4.0.12 < Via: 1.1 google < CF-Cache-Status: DYNAMIC < Strict-Transport-Security: max-age=15552000; preload < X-Content-Type-Options: nosniff < Alt-Svc: h3-23=":443"; ma=86400 < Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" < Server: cloudflare < CF-RAY: 533243e4bcd4bbf4-LHR <

限速下載

如果您正在進行優化並想查看以特定速度下載需要多長時間,您可以:-

 curl –-limit-rate 2000B

前任:

 curl –-limit-rate 2000B https://gf.dev

使用代理連接

如果您在需要使用代理連接到外部世界的 DMZ 服務器上工作,這將非常方便。

 curl --proxy yourproxy:port https://yoururl.com

帶有註入標頭的測試 URL

您可以通過在數據中插入標題來使用 curl 來測試或解決特定問題。 讓我們看以下使用 Content-Type 請求的示例。

 curl --header 'Content-Type: application/json' http://yoururl.com

通過上述操作,您要求 curl 在請求標頭中將 Content-Type 作為 application/json 傳遞。

僅顯示響應標頭

如果您正在進行一些故障排除并快速想要檢查響應標頭,則可以使用以下語法。

 curl --head http://yoururl.com

前任:

 [root@gf-lab tmp]# curl --head https://chandan.io HTTP/1.1 200 OK Date: Sat, 09 Nov 2019 19:51:23 GMT Content-Type: text/html Connection: keep-alive Set-Cookie: __cfduid=d3cb2c7b8e566ad99c870b0af12b0f1eb1573329083; expires=Sun, 08-Nov-20 19:51:23 GMT; path=/; domain=.chandan.io; HttpOnly X-GUploader-UploadID: AEnB2Uo96JhvJmR2zYUL-Ndh2ta3UD_ykQAB5C7O8cjZQhCf-GxHQ0MsodSzRnl3guSN3ywAYNjtWcPXfwDXjLg3bQ-P5vQMOA Expires: Sat, 09 Nov 2019 20:51:23 GMT Cache-Control: public, max-age=3600 Last-Modified: Mon, 06 Aug 2018 10:45:47 GMT x-goog-generation: 1533552347482034 x-goog-metageneration: 1 x-goog-stored-content-encoding: identity x-goog-stored-content-length: 24620 x-goog-hash: crc32c=DpDPAQ== x-goog-hash: md5=cIP/3rusdUx12Zla1kf1yA== x-goog-storage-class: MULTI_REGIONAL Accept-Ranges: bytes CF-Cache-Status: DYNAMIC Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Server: cloudflare CF-RAY: 53325234dc2fbb9a-LHR [root@gf-lab tmp]#

連接 HTTPS/SSL URL 並忽略任何 SSL 證書錯誤

當您嘗試訪問 SSL/TLS 證書安全 URL 時,如果該 URL 具有錯誤的證書或 CN 不匹配,那麼您將收到以下錯誤。

 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

好消息,您可以使用--insecure標誌指示 cURL 忽略證書錯誤。

 curl --insecure https://yoururl.com

使用特定協議 (SSL/TLS) 連接

測試特定 URL 是否可以通過特定 SSL/TLS 協議握手非常方便。

使用 SSL v3 連接

curl --sslv3 https://yoururl.com

對於不同的 TLS 版本

curl --tlsv1 https://example.com curl --tlsv1.0 https://example.com curl --tlsv1.1 https://example.com curl --tlsv1.2 https://example.com curl --tlsv1.3 https://example.com

從 FTP 服務器下載文件

您也可以通過指定用戶名和密碼來使用 curl 下載文件。

 curl -u user:password -O ftp://ftpurl/style.css

您始終可以使用帶有任何語法的“ -v ”以詳細模式打印。

使用主機頭

當請求的內容僅在主機標頭匹配時可用時,主機標頭可用於通過 IP 測試目標 URL。 或者,如果您想使用負載平衡器 IP/URL 測試應用程序。

 curl --header 'Host: targetapplication.com' https://192.0.0.1:8080/

在線使用 cURL 怎麼樣?

是的,使用以下工具可以做到這一點。 您可以遠程執行 cURL。

Online CURL – 一個輕量級的在線獲取 URL 的工具,並且可以添加以下選項。

 --connect-timeout --cookie --data --header --head --location --max-time --proxy --request --user --url --user-agent

cURL 命令行生成器——這個是不同的。 它可以幫助您構建 curl 命令,您可以在其中在漂亮的 UI 中輸入信息,在底部,您將獲得 cURL 命令。

捲曲生成器

cURL 是解決實時連接問題的有用實用程序,希望以上內容對您有所幫助。 如果您想了解更多信息,那麼我會推薦 Linux 命令行基礎在線課程。