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 上传文件
-# 显示进度条
No 1:curl 命令发送get请求
示例:curl -X GET http://www.xxx.com/search?data=123
No 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.xxx.com
No 3:curl 命令发送post请求
curl www.xxx.com/api/openservice/query/queryProperty -H "Content-Type:application/json" -X POST -d '{"baseCondition": {"propertyId": 1, "dateRange": {"fromDate": "2019-07-04", "toDate": "2019-07-05"}, "adults": 1,"children": 0, "rooms": 1, "channelId": 2, "sellCategories": ["PUBLIC", "NEGOTIATE"]},"extensionCondition": {"rateIds": [], "unableFlag":"False", "currency": "CNY"}, "context": {}}'
No 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.xxx.com/api/openservice/query/queryProperty -H "Content-Type:application/json" -X POST -d '{"baseCondition": {"propertyId": 1, "dateRange": {"fromDate": "2019-07-04", "toDate": "2019-07-05"}, "adults": 1,"children": 0, "rooms": 1, "channelId": 2, "sellCategories": ["PUBLIC", "NEGOTIATE"]},"extensionCondition": {"rateIds": [], "unableFlag":"False", "currency": "CNY"}, "context": {}}'
No 5:curl 命令保存文件到本地
curl -o file.html http://www.xxx.com/index.html
No 6:curl 命令登录服务
curl -u name:passwd http://www.xxx.com
No 7:curl 命令构造一个请求头
curl -H "Content-Type:application/json" http://www.xxx.com
No 8:curl 命令从文件中读取cookie 信息
curl -b "cookie.txt" http://www.xxx.com
No 9:curl 命令上传文件
curl -b "cookie.txt" -F "file=@/temp/a.txt" http://www.xxx.com/api/upload -v
No 10:curl 命令下载文件并显示下载进度
curl -# -o abc.jpg http://www.xxx.com/abc.JPG
No 11:curl 命令清除es 里面的数据
curl -XDELETE http://10.8.102.102:9200/entity-beta-ars6_ars-reservation -uadmin:admin #清理ES