curl的使用
curl是常用发起http请求工具,今天就整理下如何正确的使用curl命令,来提高工作效率。
首先我们使用curl --help命令来看下官方的使用介绍:
可以看到curl的命令格式为:curl [option] \<url\>
- 直接访问网页
$ curl http://www.baidu.com
- 访问的数据存成文件
$ curl http://www.baidu.com >>baidu.html
或
$ curl -o baidu.com http://www.baidu.com
-O 可以使用默认名称存储,可以直接保存。
curl -O https://www.baidu.com/img/baidu_jgylogo3.gif
直接下载图片成baidu_jgylogo3.gif
- 发送POST,DELETE,PUT的方法,发送自定义命令,需要使用-X这个参数。post,put ,delete 请求发送
curl -X POST http://localhost:8080/index curl -X PUT http://localhost:8080/index curl -X DELETE http://localhost:8080/index
- 增加请求参数
在请求一个网址网址的时候,常常需要加上对应的请求参数,这里需要增加一个-d的参数
curl -X POST -d '{"userName":"fuwei","blog":"blog.laofu.online"}' http://localhost:8080/index
- 增加请求头信息 curl修改header信息,需要-H参数:
curl -X POST -H 'Content-type: application/json' -d '{"userName":"fuwei","blog":"blog.laofu.online"}' http://localhost:8080/index
- 使用代理
curl -x 192.168.0.112:8080 http://localhost:8080/index
- 保存Cooike和响应数据
curl -c cookiec.txt http://localhost:8080/index
或者只用-D把响应的头存入文件中。
curl -D cookiec.txt http://localhost:8080/index
- 使用Cookie
curl -b cookiec.txt http://localhost:8080/index
- 使用User-Agent的属性
curl -A "iphone" http://localhost:8080/index
- Refer属性的添加
curl -e "www.baidu.com" http://localhost:8080/index
- 下载文件
11.1 单个下载
单个下载可以使用”-o fileName” 或者-O(默认的名称)来保存文件
curl -O https://www.baidu.com/img/baidu_jgylogo3.gif
11.2 批量下载
批量下载jpg1-5
curl -O https://www.baidu.com/jpg[1-5].JPG
11.3 断点续传
curl -C -O https://www.baidu.com/jpg1.JPG