Curl
curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP,FTPS,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE及LDAP等协议。curl支持HTTPS认证,并且支持HTTP的POST,PUT等方法,FTP上传,kerberos认证,HTTP上传,代理服务器,cookies,用户名/密码认证,通过http代理服务器上传文件到FTP服务器等.
Curl 常用参数
-I 只显示请求头信息
-d HTTP POST方式传送数据,以json格式
-o 把输出写到该文件中
-s 静默模式。不输出任何东西
-X 指定什么命令,如GET POST
-v 查看详情
-u 设置服务器的用户和密码
-H 要发送到服务端的自定义请求头
-w 完成后输出什么
-b 从文件中读取cookie信息
-F 上传文件
-# 显示进度条
Curl 常用命令
1、curl 命令发送get请求
示例:curl -X GET https://www.x.com/home?param=1
2、curl 命令发送get请求后统计各阶段耗时
curl -o /dev/null -s -w "time_namelookup:%{time_namelookup}\ntime_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" www.x.com
3、curl 命令发送post请求
curl www.x.com/api -H "Content-Type:application/json" -X POST -d '{"baseCondition": {"propertyId": 1, "dateRange": {"fromDate": "2020-05-15", "toDate": "2020-06-15"}, "adults": 1,"children": 0, "rooms": 1, "channelId": 2, "sellCategories": ["PUBLIC", "NEGOTIATE"]},"extensionCondition": {"rateIds": [], "unableFlag":"False", "currency": "CNY"}, "context": {}}'
4、curl 命令发送post请求后统计各阶段耗时
curl -o /dev/null -s -w "time_namelookup:%{time_namelookup}\ntime_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" www.x.com/api -H "Content-Type:application/json" -X POST -d '{"baseCondition": {"propertyId": 1, "dateRange": {"fromDate": "2020-05-15", "toDate": "2020-06-15"}, "adults": 1,"children": 0, "rooms": 1, "channelId": 2, "sellCategories": ["PUBLIC", "NEGOTIATE"]},"extensionCondition": {"rateIds": [], "unableFlag":"False", "currency": "CNY"}, "context": {}}'
5、curl 命令保存文件到本地
(将文件下载到本地并命名为file.html)
curl -o file.html https://www.x.com/index.html
(将文件下载到本地并命名为index.html)
curl -o https://www.x.com/index.html
(同时获取多个文件)
curl -o url1 -o url2
6、curl 命令登录服务
curl -u name:passwd https://www.x.com
# 通常的做法是在命令行只输入用户名,之后会提示输入密码,这样可以保证在查看历史记录时不会将密码泄露
curl -u username URL
7、curl 命令构造一个请求头
curl -H "Content-Type:application/json" https://www.x.com
8、curl 命令从文件中读取cookie 信息
curl -b "cookie.txt" https://www.x.com
9、curl 命令上传文件
curl -b "cookie.txt" -F "file=@/temp/test.txt" https://www.x.com/api/upload -v
10、curl 命令下载文件并显示下载进度
curl -# -o abc.jpg https://www.x.com/abc.JPG
11、curl 命令清除es 里面的数据
curl -XDELETE http://127.0.0.1:80/entity-beta-ars6_ars-reservation -uadmin:admin #清理ES
12、curl使用地址重定向,此时会查询google.com.hk站点
curl -L http://www.google.com
13、curl通过--limit-rate选项对CURL的最大网络使用进行限制
(下载速度最大不会超过1000B/second)
curl --limit-rate 1000B -O http://www.gnu.org/software/gettext/manual/gettext.html
14、下载指定时间内修改过的文件
当下载一个文件时,可对该文件的最后修改日期进行判断,如果该文件在指定日期内修改过,就进行下载,否则不下载。
该功能可通过使用-z选项来实现
# 若yy.html文件在2011/12/21之后有过更新才会进行下载
curl -z 21-Dec-11 http://www.example.com/yy.html
15、从FTP服务器下载文件
CURL同样支持FTP下载,若在url中指定的是某个文件路径而非具体的某个要下载的文件名,CURL则会列出该目录下的所有文件名而并非下载该目录下的所有文件
# 列出public_html下的所有文件夹和文件
curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/
# 下载xss.php文件
curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php
16、上传文件到FTP服务器
通过 -T 选项可将指定的本地文件上传到FTP服务器上
# 将myfile.txt文件上传到服务器
curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com
# 同时上传多个文件
curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com
# 从标准输入获取内容保存到服务器指定的文件中
curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt
17、获取更多信息
通过使用 -v 和 -trace获取更多的链接信息
通过字典查询单词
# 查询bash单词的含义
curl dict://dict.org/d:bash
# 列出所有可用词典
curl dict://dict.org/show:db
# 在foldoc词典中查询bash单词的含义
curl dict://dict.org/d:bash:foldoc
18、为CURL设置代理
-x 选项可以为CURL添加代理功能
# 指定代理主机和端口
curl -x proxysever.test.com:3128 http://google.co.in
19、保存与使用网站cookie信息
# 将网站的cookies信息保存到sugarcookies文件中
curl -D sugarcookies http://localhost/sugarcrm/index.php
# 使用上次保存的cookie信息
curl -b sugarcookies http://localhost/sugarcrm/index.php
20、传递请求数据
默认curl使用GET方式请求数据,这种方式下直接通过URL传递数据
可以通过 --data/-d 方式指定使用POST方式传递数据
# GET
curl -u username https://api.github.com/user?access_token=XXXXXXXXXX
# POST
curl -u username --data "param1=value1¶m2=value" https://api.github.com
# 也可以指定一个文件,将该文件中的内容当作数据传递给服务器端
curl --data @filename https://github.api.com/authorizations