curl 命令详解
curl 是在命令行下工作的文件传输工具
curl 场景使用
一、http方法
1、在每一个http请求中,都有一个对应的方法,方法有:GET、POST、HEAD和PUT.
2、curl默认情况下,就是get方法
二、header
在curl中,使用-i选项可以显示RESPONSE的header信息,连同Body数据
curl -i www.baidu.com
使用-I选项可以只显示response的Header信息
curl -I www.baidu.com
三、POST
post是HTTP中向服务端提交数据的而一种方法
1.在curl中,我们可以使用-d或-data选项来指定具体的数据
curl -d key1=value1 & key2=value2 http://test.com
2.在curl 中,我们多个-d指定多组数据,会自动吧数据连起来
curl -d key1=value1 -d key2=value2 http://test.com
3.数据过多,把数据存放到文件中
curl -d @filename http://test.com
content-type
使用post方法提交数据时,主要有以下四种类型:
• application/x-www-form-urlencoded: 默认的形式,即key1=value&key2=value2 的形式
• multipart/form-data: 上传文件的形式
• application/json: 提交json数据
• text/xml: 提交xml数据
content-type 是一个Header,不指定的话,默认就是application/x-www-form-urlencoded形式传输数据
curl -d '{test test test}' -H 'Content-Type: application/json' http://test.com
post 一个二进制数据
curl --data-binary @filename http://test.com
转化成一个get
curl -d "key1=value1" -G http://test.com #转化成url是:http://test.com/?key1=value1
url编码
curl --data-urlencode "name=Alan Walker" http://test.com
multipart formposts
为了可以在curl中模拟这个请求,我们可以使用-F或-form选项来指定数据:
curl -F person=annonymous -F secret=@filename http://test.com/submit.cgi
其中Content-Type和enctype一致的。
当使用-F 选项时,默认的Content-Type就是multipart/form-data,不过,我们可以使用-H进行指定:
curl -F 'name=Dan' -H 'Conten-Type: multipart/magic' https://test.com
HTTP重定向
curl:redirect
在curl 中,默认不会重定向,可以使用-L选项来告诉curl重定向
curl -L http://test.com
GET还是POST
第一次不是GET方法,我们可以使用-post301,-post302和-post303选项来指定
修改HTTP请求
1、在请求行中包含这次请求所使用的方法。
curl http://test.com/file
2、在HTTP方法中我们通过指定什么方法,使用-X来进行指定
curl -X POST http://test.com
3、修改请求头
▪ 在curl中,指定-H指定Content-Type,其实Header就是一个key:valu对:
curl -H "HeaderName: HeaderValue" http://test.com
▪ referer 从哪里跳转过来的:
curl --referer http://test.com http://test1.com
▪ User Agent
这个字段是用来表示客户端的设备信息的
curl --user-agent "[User Agent]" http://test.com
▪ Cookies
HTTP是一种无状态的协议,可以使用Cookies,服务器通过set-Cookie:来设置Cookie
curl --cookie "CookieName=CookieValue" http://test.com
▪ 从文件中读取Cookies
curl -b cookies.txt http://test.com
▪ 写cookie 到文件
curl -c cookies.jar.txt http://test.com
▪ 我们从文件中读取Cookies,也需要保存Cookies
curl -b cookies.txt -c cookie.jar.txt http://test.com
curl 参数详解
▪ -A 参数指定客户端的用户代理表头,即User-Agent
curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' http://test.com
▪ -b 参数用来向服务器发送Cookie
curl -b 'foo=bar' http://test.com
▪ -G参数用来构造URL的查询字符串
curl -G -d 'q=kitties' -d 'count=20' http://test.com
▪ 数据需要URL编码,可以结合-data-urlencode参数
curl -G --data-urlencode 'comment=hello world' https://test.com
▪ -H 参数添加HTTP请求的标头
curl -H 'Accept-Language: en-US' http://test.com
▪ -k参数指定跳过ssl检测
curl -k https://test.com
▪ -L参数会让HTTP请求跟随服务器的重定向
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
▪ -limit-rate 用来限制HTTP请求和回应的宽带
curl --limit-rate 300k https://test.com
▪ -s参数将不输出错误和进度信息
curl -s htps://test.com
▪ -S参数指定只输出错误信息,通常与-s一起使用
curl -s -o /dev/dull http://test.com
▪ -v 输出通信整个过程,用于调试
curl -v https://test.com
▪ -u参数用来设置服务器认证的用户名和密码
curl -u 'bob:12345' https://test.com/login