curl 在测试http 请求非常有用,可以访问下载,也可以模拟浏览器,支持多种协议, 是个必备的技能
参数
-
-A 添加ua
-
-b filename 读取cookie的地方
-
-o filename 把网页保存到文件中
-
-d data POST请求参数
-
-I 只查看请求头信息
-
-v 显示通信过程
-
-H 自定义请求头
-
-s 静默模式,不显示什么信息
-
-w "xxxx" 请求完成后打印信息
-
-x host:port 使用代理
-
-c 保存cookie的地方
-
-r 支持断点续传
-
-L 跟踪重定向
-
-0 或者 --http1.0 使用http1.0
-
-r range请求
案例
对照上面的请求参数,理解下面常用的例子,大部分的场景都能覆盖(很多场景有几种写法,这里没有列全,可以curl -h 自行补充)
这里基本都是开发用到的,curl远不止这些功能,还有代理,SSL, 限速,文件上传等等非常多的功能
下载一个网页
模拟get请求
-
curl http://baidu.com/?wd=curl
模拟post请求
-
curl -d "pass=cc&name=vv" http://baidu.com
只看请求头
-
curl -I http://baidu.com
查看通信过程
-
curl -v orangleliu.info
自定义请求头
-
curl -I -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" www.baidu.com
-
curl --user-agent "[User Agent]" -I baidu.com
自定义reffer
-
curl -I -e i-erya.com baidu.com
请求使用gzip
-
curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte
测试连接网站的速度
-
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.baidu.com"
断点续传 下载
-
curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html
GET方法 http1.0版本
-
curl -X GET -0 -I 'http://124.88.52.98:3698/account/ulogin/4/?wlanuserip=10.14.186.63&wlanacname=&ssid=eWiFi-JS&wlanparameter=68-96-7b-ef-f5-72'
basic auth 自定义header
-
curl -u sso:sso -H 'Host:api.sso.modocker.com' 'http://47.93.239.182/api/wechat/qrcode' -i
curl 模拟 multipart/form-data 表单
注意每个k v都要用一个 -F
-
curl -v -X post -F "data=@dump.rdb" -F "bucket=test" -F "key=test1" -F "md5=xxx" 'http://localhost:5003/upload/key'
多个文件上传
-
$ curl -F 'fileX=@/path/to/fileX' -F 'fileY=@/path/to/fileY' ... http://localhost/upload
上传一组文件,也是多个文件上传
-
$ curl -F 'files[]=@/path/to/fileX' -F 'files[]=@/path/to/fileY' ... http://localhost/upload
PUT 方式上传文件
-
curl -v --upload-file dump.rdb 'http://localhost:5003/upload/key'
发送jsonin 请求
-
$ curl --header "Content-Type: application/json" \
-
--request POST \
-
--data '{"username":"xyz","password":"xyz"}' \
-
http://localhost:3000/api/login