GET
$ curl douban.com<html><head><title>301 Moved Permanently</title></head><body><center><h1>301 Moved Permanently</h1></center><hr><center>nginx</center></body></html>
可以看见,上述返回 301 永久重定向,可以使用 curl 中的 -i/--include 选项,返回完整的 HTTP Response 信息:
$ curl -i douban.comHTTP/1.1 301 Moved PermanentlyDate: Thu, 19 Nov 2020 02:31:39 GMTContent-Type: text/htmlContent-Length: 162Connection: keep-aliveKeep-Alive: timeout=30Location: http://www.douban.com/Server: daeStrict-Transport-Security: max-age=15552000;<html><head><title>301 Moved Permanently</title></head><body><center><h1>301 Moved Permanently</h1></center><hr><center>nginx</center></body></html>
如果你只想返回头信息,可以使用 -I/--head:
$ curl -I douban.comHTTP/1.1 301 Moved PermanentlyDate: Thu, 19 Nov 2020 02:34:17 GMTContent-Type: text/htmlContent-Length: 162Connection: keep-aliveKeep-Alive: timeout=30Location: http://www.douban.com/Server: daeStrict-Transport-Security: max-age=15552000;
从头信息中我们可以看出,他被重定向到了 http://www.douban.com/ 上,我们可以使用 -L/--location 自动重定向:
$ curl -L douban.com... # 内容较多,这里省略
下载文件
由于上面输出内容较多,我们使用 -o/--output 来将输出的内容下载成文件:
$ curl -o douban.html douban.com% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed100 162 100 162 0 0 1408 0 --:--:-- --:--:-- --:--:-- 1396
也可以指定 -O 来下载文件,他会自动以末尾的 URI 文件名命名,所以如果 URI 末尾没有文件名,他将会失败:
$ curl -O https://wangdoc.com/bash/intro.html% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed100 21700 0 21700 0 0 7209 0 --:--:-- 0:00:03 --:--:-- 7211
显示通信过程
如果你想更详细的查看网络的通信过程,可以使用 -v/--verbose ,也称啰嗦模式
$ curl -v douban.com* Trying 154.8.131.171:80...* Connected to douban.com (154.8.131.171) port 80 (#0)> GET / HTTP/1.1> Host: douban.com> User-Agent: curl/7.73.0> Accept: */*>* Mark bundle as not supporting multiuse< HTTP/1.1 301 Moved Permanently< Date: Thu, 19 Nov 2020 03:04:10 GMT< Content-Type: text/html< Content-Length: 162< Connection: keep-alive< Keep-Alive: timeout=30< Location: http://www.douban.com/< Server: dae< Strict-Transport-Security: max-age=15552000;<<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 douban.com left intact
也可以使用 --trace 和 --trace-ascii 查看更详细的信息:
$ curl --trace output.txt douban.com$ curl --trace-ascii output.txt douban.com # 以 ascii 码的形式输出
POST 方法
可以直接使用 -d 来使用使用 POST 方法来传递数据:
比如,http://www.formpost.com/getthis/ 网站上有这样一个表单:
<form action="post.cgi" method="post"><input name=user size=10><input name=pass type=password size=10><input name=id type=hidden value="blablabla"><input name=ding value="submit"></form>
就可以使用下面这段命令来请求:
$ curl -d "user=foobar&pass=12345&id=blablabla&ding=submit"http://www.formpost.com/getthis/post.cgi
-d 选项其实会自动加上 HTTP 请求头:Content-Type : application/x-www-form-urlencoded,并将其自动转化为 POST 方法,因此可以省略 -X POST。
可以使用非简写的命令:
$ curl --header "Content-Type: application/x-www-form-urlencoded" \--request POST \--data "user=foobar&pass=12345&id=blablabla&ding=submit" \http://www.formpost.com/getthis/post.cgi
添加 HTTP 请求标头
从上面可以看出,使用 -H/--header 可以添加 HTTP 请求标头:
$ curl -H 'Accept-Language: en-US' https://google.com
添加数据
从上面可以看出,使用 --data 可以传递数据:
$ curl --data 'comment=hello%20world' https://google.com/login
也可以使用 --data-urlencode 自动进行 URL 编码:
$ curl --data-urlencode 'comment=hello world' https://google.com/login
注意这里的
hello world中间的空格不用自己编码
样例
$ curl -v --noproxy "*" www.baidu.com
其中:
-v可以更详细的查看网络通信过程。--noproxy "*"表示在请求过程中禁用代理。
