curl的使用
curl是一个命令行工具和库,用于通过URL传输数据
支持的通信协议非常多:
DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT等等
1.安装
Ubuntu上安装
sudo apt install curl
centos上安装
sudo yum install curl
windows上
安装git,Git Bash 自带 cURL
2.使用
(1)直接请求URL
eg:
$ curl http://wttr.in/
(2)直接请求URL
-o
参数将服务器的回应保存成文件
eg:
$ curl -o aa.txt http://wttr.in/
会将刚才输出的内容,保存到 aa.txt中
保存图片
$ curl -o tmp.png 'https://img2020.cnblogs.com/blog/798214/202006/798214-20200612102237381-1152081887.png'
-s
参数禁用进度表
$ curl -o tmp.png 'https://img2020.cnblogs.com/blog/798214/202006/798214-20200612102237381-1152081887.png' -s
--process-bar
参数让进度表显示为进度条
$ curl -o tmp.png 'https://img2020.cnblogs.com/blog/798214/202006/798214-20200612102237381-1152081887.png' --progress-bar
-i
参数显示 Response Headers 信息
$ curl -i -X GET http://localhost:8080/user/detail?id=1 -s HTTP/1.1 200 Access-Control-Allow-Methods: GET,POST,OPTIONS,PUT,DELETE Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers Content-Type: application/json Transfer-Encoding: chunked Date: Mon, 15 Jun 2020 04:34:01 GMT {"code":200,"message":"SUCCESS","data":{"id":1,"name":"admin","nickName":"超管","email":"admin@qq.com","mobile":"13612345678","status":1}}
-I
只显示 Response Headers 信息
$ curl -I -X GET http://localhost:8080/user/detail?id=1 -s HTTP/1.1 200 Access-Control-Allow-Methods: GET,POST,OPTIONS,PUT,DELETE Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers Content-Type: application/json Transfer-Encoding: chunked Date: Mon, 15 Jun 2020 04:32:13 GMT
-X
指定请求方式
GET
$ curl -X GET http://localhost:8080/user/detail?id=1 -s {"code":200,"message":"SUCCESS","data":{"id":1,"name":"admin","nickName":"超管","email":"admin@qq.com","mobile":"13612345678","status":1}}
POST
参数为form数据
$ curl -X POST -d "id=1" http://localhost:8080/user/detail -s {"code":200,"message":"SUCCESS","data":{"id":1,"name":"admin","nickName":"超管","email":"admin@qq.com","mobile":"13612345678","status":1}}
多个参数
$ curl -X POST -d "id=1&change=1" http://localhost:8080/user/detail -s {"code":200,"message":"SUCCESS","data":{"id":1,"name":"admin","nickName":"超管","email":"admin@qq.com","mobile":"13612345678","status":1}}
参数为json数据
$ curl -H "Content-Type: application/json" -X POST -d '{"name":"abc","nickName":"abc123"}' http://localhost:8080/user/add -s {"code":200,"message":"SUCCESS","data":{"id":33,"name":"abc","nickName":"abc123","email":null,"mobile":null,"status":null}}
-d
或--data
用来指定传递的数据
-H
用来指定Header,有四种方式
application/x-www-form-urlencoded:默认的形式,即key1=value1&key2=value2的形式 multipart/form-data:使用表单上传文件时使用这个形式 application/json:提交JSON格式的数据 text/xml:提交XML格式的数据
-F
参数用来向服务器上传二进制文件
$ curl -H "multipart/form-data" -X POST -F "file=@test.ifc" http://localhost:8080/upload/fullFile -s {"code":200,"message":"上传成功","data":{"id":14,"name":"20200615122911704.txt","size":15778,"status":1,"uploadTime":"2020-06-15 12:29:11"}}