Curl简单实践
url 是一个通过网络协定传输数据的指令,用很白话的说法就是让你「连接到网站」。

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


上次修改於 2022-11-04