软件测试中curl 的常见用法

一、简介
curl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。做网页处理流程和数据检索自动化,curl可以祝一臂之力。它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。
二、参数用法
1、基本用法
不带有任何参数时,curl 就是发出 GET 请求。
[root@localhost ~]# curl www.baidu.com
上面命令向www.baidu.com发出 GET 请求,服务器返回的内容会在命令行输出:
<!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> .... <div href=http://www.baidu.com/duty/>使用百度前必读</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
2、-A参数
-A参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]。
[root@localhost ~]# curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36' http://www.baidu.com
上面命令将User-Agent改成 Chrome 浏览器。
[root@localhost ~]# curl -A '' http://www.baidu.com
上面命令会移除User-Agent标头。
也可以通过-H参数直接指定标头,更改User-Agent。
[root@localhost ~]# curl -H 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1' http://www.baidu.com
3、-b参数
-b参数用来向服务器发送 Cookie。
[root@localhost ~]# curl -b 'foo=bar' www.baidu.com
上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。
[root@localhost ~]# curl -b 'foo1=bar;foo2=bar2' www.baidu.com
上面命令发送两个 Cookie。
[root@localhost ~]# curl -b cookies.txt www.baidu.com
上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。
4、-c参数
-c参数将服务器设置的 Cookie 写入一个文件。
[root@localhost ~]# curl -c cookies.txt https://www.google.com
上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
5、-d参数
-d参数用于发送 POST 请求的数据体。
[root@localhost ~]# curl -d'login=emma&password=123'-X POST https://google.com/login # 或者 [root@localhost ~]# curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login
使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST。
-d参数可以读取本地文本文件的数据,向服务器发送。
[root@localhost ~]# curl -d '@data.txt' https://google.com/login
上面命令读取data.txt文件的内容,作为数据体向服务器发送。
6、--data-urlencode参数
--data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
[root@localhost ~]# curl --data-urlencode 'comment=hello world' https://google.com/login
上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。
7、-e参数
-e参数用来设置 HTTP 的标头Referer,表示请求的来源。
[root@localhost ~]# curl -e 'https://google.com?q=example' https://www.example.com
上面命令将Referer标头设为https://google.com?q=example。
-H参数可以通过直接添加标头Referer,达到同样效果。
[root@localhost ~]#curl -H 'Referer: https://google.com?q=example' https://www.example.com
8、-F参数
-F参数用来向服务器上传二进制文件。
[root@localhost ~]# curl -F 'file=@photo.png' https://google.com/profile
上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。
-F参数可以指定 MIME 类型。
[root@localhost ~]# curl -F 'file=@photo.png;type=image/png' https://google.com/profile
上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。
-F参数也可以指定文件名。
[root@localhost ~]# curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。
9、-G参数
-G参数用来构造 URL 的查询字符串。
[root@localhost ~]# curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
上面命令会发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略--G,会发出一个 POST 请求。
如果数据需要 URL 编码,可以结合--data--urlencode参数。
[root@localhost ~]# curl -G --data-urlencode 'comment=hello world' https://www.example.com
10、-H参数
-H参数添加 HTTP 请求的标头。
[root@localhost ~]# curl -H 'Accept-Language: en-US' https://google.com
上面命令添加 HTTP 标头Accept-Language: en-US。
[root@localhost ~]# curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
上面命令添加两个 HTTP 标头。
[root@localhost ~]# curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
上面命令添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。
11、-i参数
-i参数打印出服务器回应的 HTTP 标头。
[root@localhost ~]# curl -i https://www.example.com
上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。
12、-I参数
-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。
[root@localhost ~]# curl -I https://www.example.com
上面命令输出服务器对 HEAD 请求的回应。
--head参数等同于-I。
[root@localhost ~]# curl --head https://www.example.com
13、-k参数
-k参数指定跳过 SSL 检测。
[root@localhost ~]# curl -k https://www.example.com
上面命令不会检查服务器的 SSL 证书是否正确。
14、-L参数
-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
[root@localhost ~]# curl -L -d 'tweet=hi' https://api.twitter.com/tweet
15、--limit-rate
--limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。
[root@localhost ~]# curl --limit-rate 200k https://google.com
上面命令将带宽限制在每秒 200K 字节。
16、-o参数
-o参数将服务器的回应保存成文件,等同于wget命令。
[root@localhost ~]# curl -o example.html https://www.example.com
上面命令将www.example.com保存成example.html。
17、-O参数
-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。
[root@localhost ~]# curl -O https://www.example.com/foo/bar.html
上面命令将服务器回应保存成文件,文件名为bar.html。
18、-s参数
-s参数将不输出错误和进度信息。
[root@localhost ~]# curl -s https://www.example.com
上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。
如果想让 curl 不产生任何输出,可以使用下面的命令。
[root@localhost ~]# curl -s -o /dev/null https://google.com
19、-S参数
-S参数指定只输出错误信息,通常与-s一起使用。
[root@localhost ~]# curl -s -o /dev/null https://google.com
上面命令没有任何输出,除非发生错误。
20、-u参数
-u参数用来设置服务器认证的用户名和密码。
[root@localhost ~]# curl -u 'bob:12345' https://google.com/login
上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。
curl 能够识别 URL 里面的用户名和密码。
[root@localhost ~]# curl https://bob:12345@google.com/login
上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。
[root@localhost ~]# curl -u 'bob' https://google.com/login
上面命令只设置了用户名,执行后,curl 会提示用户输入密码。
21、-v参数
-v参数输出通信的整个过程,用于调试。
[root@localhost ~]# curl -v https://www.example.com
--trace参数也可以用于调试,还会输出原始的二进制数据。
[root@localhost ~]# curl --trace - https://www.example.com
22、-x参数
-x参数指定 HTTP 请求的代理。
[root@localhost ~]# curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
上面命令指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。
如果没有指定代理协议,默认为 HTTP。
[root@localhost ~]# curl -x james:cats@myproxy.com:8080 https://www.example.com
上面命令中,请求的代理使用 HTTP 协议。
23、-X参数
-X参数指定 HTTP 请求的方法。
[root@localhost ~]# curl -X POST https://www.example.com
上面命令对https://www.example.com发出 POST 请求。
三、使用场景
1、软件测试Session
如果后端使用 session 记录使用者登入信息,那么后端通常会传一个 session ID 给前端。之后,前端在发给后端的 requests 的 header 中就需要设置此 session ID,后端便会以此 session ID 识别出前端是属于具体哪个 session,命令如下所示:
[root@localhost ~]#curl -i -H "sessionid:XXXXXXXXXX" -X GET "http://XXX/api/demoAPI"
2、软件测试Cookie
如果是使用 cookie,在认证成功后,后端会返回 cookie 给前端,前端可以把该 cookie 保存成为文件,当需要再次使用该 cookie 时,再用“-b cookie_File” 的方式在 request 中植入 cookie 即可正常使用。命令如下所示:
// 将cookie保存为文件 [root@localhost ~]#curl -i -X POST -d username=robin -d password=password123 -c ~/cookie.txt "http://XXX/auth" // 载入cookie到request中 [root@localhost ~]#curl -i -H "Accept:application/json" -X GET -b ~/cookie.txt "http://XXX/api/demoAPI"
3、文件下载
选项-o将下载数据写入到指定名称的文件中,并使用--progress显示进度条:
[root@localhost ~]# curl www.baidu.com -o baidu.txt --progress
######################################################################## 100.0%
[root@localhost ~]# cat baidu.txt 
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper>
...
<a href=http://www.baidu.com/duty/>使用百度前必读</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
4、模拟各种网络环境
使用--limit-rate限制curl的下载速度:
[root@localhost ~]# curl URL --limit-rate 50k
命令中用k(千字节)和m(兆字节)指定下载速度限制。
使用--max-filesize指定可下载的最大文件大小:
[root@localhost ~]# curl URL --max-filesize bytes
如果文件大小超出限制,命令则返回一个非0退出码,如果命令正常则返回0。

                
            
        
浙公网安备 33010602011771号