curl 是一個透過網路協定傳輸資料的指令,用很白話的說法就是讓你「連線到網站」。

基本使用

curl http://www.example.com

這樣會將 response body print 出來,為了要快速驗證網站的回應是否正常,我比較常用的形式是

curl -iL http://www.example.com

-i 代表顯示 header + body ,如果只要 header 的話,可以使用大寫的 I 。

-L 則代表跟隨網址的 redirect 規則。例如說,現在的主流網站用 HTTP 連線會自動轉成 HTTPs 就可以透過 curl 來觀察。

curl -IL http://google.com

---

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Mon, 30 Aug 2021 23:42:39 GMT
Expires: Wed, 29 Sep 2021 23:42:39 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Date: Mon, 30 Aug 2021 23:42:39 GMT
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
Expires: Mon, 30 Aug 2021 23:42:39 GMT
Cache-Control: private

以 google 為例,你可以看到第一個 request 的 header 是 301 Move Permanently,且包含了 Location ,有加上 -L 參數的話 curl 就會跟隨 redirect 的location 繼續 request。

輸出更多訊息

可以用 -v 或 –verbose 讓 curl 輸出更詳細的訊息,我都拿來看它實際上連線到哪一台主機

curl -v https://example.com

curl --verbose https://example.com

~ ❯ curl -v https://example.com                                                                            at 08:04:25
*   Trying 93.184.216.34...
* TCP_NODELAY set
* Connected to example.com (93.184.216.34) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):

…下略

指定要連線的主機

curl 預設會使用 DNS resolve 來決定要使用哪一台主機。

CDN 的開發上,常會需要將 request 送到特定的主機,可以用修改 host 檔的方式,也可以用參數 –resolve 直接將 DNS resolve 加進參數,方便程式化。

curl --resolve google.com:443:127.0.0.1 --resolve google.com:80:127.0.0.1 https://google.com -v

* Added google.com:443:127.0.0.1 to DNS cache
* Added google.com:80:127.0.0.1 to DNS cache
* Hostname google.com was found in DNS cache
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connection failed
* connect to 127.0.0.1 port 443 failed: Connection refused
* Failed to connect to google.com port 443: Connection refused
* Closing connection 0

連線到 google.com ,80 port 及 443 port 都連線到 127.0.0.1 。加上 -v 就可以看到 curl 確實將 request 送到 127.0.0.1。

用指定 Header 的方式也可以達成類似的效果:

curl -H "Host: www.google.com" http://127.0.0.1/path/to/resource

但使用時要注意這樣跟 client 實際送出的 request 是不完全相同的,如果用的是 HTTPs , certificate 就不能順利生效,可以配合 -k 使用。

比較適合快速驗證,要完整的話還是用 –resolve 為佳。

忽略 SSL 憑證

如果要連線的網站憑證過期,或是因為其他原因你並不在乎憑證是否有效,可以使用 -k (或是更有表達力的 –insecure )

# 試著 curl 一個憑證過期的網站,會產生錯誤
curl https://certificate.expired.net/

curl: (60) SSL certificate problem: certificate has expired
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

# 視需求,你可能並不在乎憑證時,可以略過它
curl -k https://certificate.expired.net/

curl --insecure https://certificate.expired.net/

使用不同的 Request Method

curl 預設使用 GET ,有時候可能會需要以 POST 或其他不同的 method 發送 request

curl -X POST https://example.com

顯示花費時間

如果需要時間資訊,可以使用 -w

curl -I example.com -w %{time_connect}-%"{time_starttransfer}-%{time_total}\n"

man curl 可以看到更多能用的 variables

最後修改日期: 2021-10-22

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。