curl命令是一个利用URL规则在命令行下工作的文件传输工具。作为一款强力工具,curl支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。
1. 获取页面内容
当不加任何参数时,默认会发送GET请求。
[yuzhan@ home]$ curl http://172.24.145.95:8080
You sen a get request
2. 显示HTTP头
-I 或者 -i 选项(-I 不显示文件内容,-i 显示文件内容)
[yuzhan@ home]$ curl -I http://172.24.145.95:8080
HTTP/1.1 405 Method Not Allowed
Server: TornadoServer/4.5.2
Content-Type: text/html; charset=UTF-8
Date: Sat, 29 Sep 2018 08:02:44 GMT
Content-Length: 87
[yuzhan@ home]$ curl -i http://172.24.145.95:8080
HTTP/1.1 200 OK
Server: TornadoServer/4.5.2
Content-Type: text/html; charset=UTF-8
Date: Sat, 29 Sep 2018 08:02:50 GMT
Etag: "2d89564b36d69043f78e54860dcbec70370929f3"
Content-Length: 22
You sen a get request
3. 将链接保存到文件
-o 或者 -O选项(-o 指定文件名, -O URL 中的文件名会被用作保存输出的文件名 )
[yuzhan@ home]$ curl -o test.html http://172.24.145.95:8080
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 22 100 22 0 0 3986 0 --:--:-- --:--:-- --:--:-- 4400
4. 自定义User-Agent
-A 自定义用户代理
[yuzhan@ home]$ curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://172.24.145.95:8080
You sen a get request
5. 自定义header
-H 自定义header
[yuzhan@ home]$ curl -H "Accept: application/json" -H "Content-Type: application/json" http://172.24.145.95:8080
You sen a get request
6. 发送post请求
-d 用于指定发送的数据,-X 用于指定发送数据的方式(-d的情况下,默认也为POST)
[yuzhan@ home ]$ curl -d "userName=yuzhan&passwd=123" -X POST http://172.24.145.95:8080/infor
You sen a post request
7. 发送Json数据
[yuzhan@ home]$ curl -H "Content-Type: application/json" -X POST -d '{"name": "yuzhan", "age" : 18}' http://172.24.145.95:8080
另外,关于CURL更全面的教材,可参考http://man.linuxde.net/curl