curl命令是一个功能强大的http命令行工具,在网站开发中能够帮助我们快速调试。下面总结下curl常用的功能。
1.发送请求
curl最基本的功能,输入要请求的url,会默认向该地址发送get请求并将返回值打印到终端上。如下所示:
1 | curl www.baidu.com |
1 |
|
如果要将返回的数据保存下来可以使用-o参数
1 | curl -o [文件名] www.baidu.com |
2.显示返回头信息
使用-i参数可以显示response的头部信息
1 | curl -i www.baidu.com |
1 | HTTP/1.1 200 OK |
3.显示完整的请求过程
使用-v参数可以显示本次请求的完整通信过程及request信息和response信息。
1 | crul -v www.baidu.com |
1 | Rebuilt URL to: www.baidu.com/ |
4.发送不同类型的请求
使用-X参数可以指定发送请求的类型
发送post请求
1 | curl -X POST www.baidu.com |
发送delete请求
1 | curl -X DELETE www.baidu.com |
5.发送表单数据
当使用post类型发送数据时需要用到–data参数,用来添加要发送的数据。
1 | curl -X POST --data="xxx" www.baidu.com |
curl还能都对数据进行表单编码
1 | curl -X POST --data-urlencode="xxx" www.baidu.com |
6.文件上传
使用curl进行文件上传
1 | curl --form upload=@localfilename --form press=OK [URL] |
7.referer字段
当你需要在http请求头中设置Referer字段时可以使用–referer参数
1 | curl --referer http://www.example.com http://www.example.com |
8.设置user-agent
当需要设置http请求头中的user-agent字段时可以使用–user-agent参数
1 | curl --user-agent "[User Agent]" [URL] |
9.设置cookie
当需要发送cookie请求时可以使用–cookie参数
1 | curl --cookie "name=xxx" www.example.com |
10.增加header信息
需要在header中添加自定义信息时可以使用–header参数
1 | curl --header "Content-Type:application/json" http://example.com |
11.http认证
当遇到需要进行http认证的网站时可以使用–user参数
1 | curl --user username/password www.baidu.com |