curl,强大的调试开发工具。

curl,你好强大。
curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。

查看源码

直接在curl命令后加上网址,就可以看到网页源码。这里就用我的博客首页来做示范,因为挺短的。

1
2
3
4
5
6
7
8
$ curl laplacence.github.io
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

加上 -o 保存则是进行保存,同等与wget。

1
2
3
4
$ curl -o <filename> laplacence.github.io
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 162 100 162 0 0 2700 0 --:--:-- --:--:-- --:--:-- 2700

自动跳转

有的网址是自动跳转的,比如我的首页。使用 -L 参数,curl就会跳转到新的网址。

1
2
3
4
5
6
7
8
$ curl -L laplacence.github.io
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<title>Trash Can</title>
MORE

显示header

-i 可以显示response header的信息,-I 则只显示header。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ curl -i laplacence.github.io
HTTP/1.1 301 Moved Permanently
Server: GitHub.com
Content-Type: text/html
Location: https://laplacence.github.io/
X-GitHub-Request-Id: 8096:4436:D7CE4:117F57:5F73E5A0
Content-Length: 162
Accept-Ranges: bytes
Date: Wed, 30 Sep 2020 02:08:31 GMT
Via: 1.1 varnish
Age: 766
Connection: keep-alive
X-Served-By: cache-hkg17933-HKG
X-Cache: HIT
X-Cache-Hits: 1
X-Timer: S1601431711.261853,VS0,VE1
Vary: Accept-Encoding
X-Fastly-Request-ID: 61661b909bca09baaec268b4e7f0a60b317e424f

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

通信过程

-v 可以显示完整的http通信过程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$ curl -v laplacence.github.io
* Trying 185.199.109.153:80...
* Connected to laplacence.github.io (185.199.109.153) port 80 (#0)
> GET / HTTP/1.1
> Host: laplacence.github.io
> User-Agent: curl/7.72.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Server: GitHub.com
< Content-Type: text/html
< Location: https://laplacence.github.io/
< X-GitHub-Request-Id: 8096:4436:D7CE4:117F57:5F73E5A0
< Content-Length: 162
< Accept-Ranges: bytes
< Date: Wed, 30 Sep 2020 02:28:30 GMT
< Via: 1.1 varnish
< Age: 1965
< Connection: keep-alive
< X-Served-By: cache-hkg17926-HKG
< X-Cache: HIT
< X-Cache-Hits: 1
< X-Timer: S1601432910.418005,VS0,VE1
< Vary: Accept-Encoding
< X-Fastly-Request-ID: 0dfc647792c349d178a666c0476557dbcee996d0
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host laplacence.github.io left intact

HTTP方法

curl默认使用GET,-X 后面可以指定其他方法。

1
2
3
4
5
6
7
$ curl -X POST laplacence.github.io
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
</body>
</html>

上传文件

假设网页的上传表单如下

1
2
3
4
  <form method="POST" enctype='multipart/form-data' action="upload.cgi">
    <input type=file name=upload>
    <input type=submit name=press value="OK">
  </form>

则可以使用这种方法。

1
2
$ curl --form upload=@<filename> --form press=OK [URL]
......

UA/header/cookie

1
2
3
4
5
6
$ curl --user-agent "UA" [URL]
......
$ curl --cookie "cookie" [URL]
......
$ curl --header "header" [URL]
......