Curl命令可以通过命令行的方式,执行Http请求.
在官网下载工具包 :https://curl.haxx.se/download.html
环境变量配置
在系统高级环境变量中,配置
CURL_HOME ----- "你的curl目录位置\curl-7.59.0"
path ---- 末尾添加 ";%CURL_HOME%\I386"
然后就可以在DOS窗口中任意位置,使用curl命令了。 可以使用curl --help 查看
Nodejs 格式化
安装json命令
npm install -g json
在curl命令后面添加 | json 即可。
curl http://localhost/Wechat/Admin/UserManagement/getUserList | json
Python 格式化
在curl命令后面添加 | python -m json.tool 即可。
curl http://localhost/Wechat/Admin/UserManagement/getUserList | python -m json.tool
如果不想显示curl的统计信息,添加 -s参数即可。
curl http://localhost/Wechat/Admin/UserManagement/getUserList -s | python -m json.tool
常用命令
下载单个文件,默认将输出打印到标准输出中
curl http://localhost/KingsMap/index.html
通过-o/-O选项保存下载的文件到指定的文件中:
-o:将文件保存为命令行中指定的文件名的文件中
-O:使用URL中默认的文件名保存文件到本地
curl -o curlindex.html http://localhost/KingsMap/index.html
curl -O http://localhost/KingsMap/demo.html
Basic Authentication
在访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权
curl -u account:password http://localhost:5566/LBSCore/BadgeItems
curl -u account http://localhost:5566/LBSCore/BadgeItems
从FTP服务器上下载文件
CURL同样支持FTP下载,若在url中指定的是某个文件路径而非具体的某个要下载的文件名,CURL则会列出该目录下的所有文件名而并非下载该目录下的所有文件
列出public_html下的所有文件夹和文件
curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/
下载inde.html文件
curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/index.html
上传文件到FTP服务器
将README.md 上传到服务器
curl -u ftpuser:ftppass -T README.md ftp://ftp_server
设置代理
curl -X GET -i http://localhost/KingsMap/3DBuilding | json
curl -X GET -i http://localhost/KingsMap/3DBuilding?P=1&LIMIT=10 | json
POST
curl -X POST -i http://localhost/KingsMap/3DBuilding --data‘buildingId=1&mapId=2’
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' -i http://localhost/WechatMedical/Admin/Login/Login --data 'username=admin&password=rootuser' | json
PUT
curl -X PUT -H 'Content-Type: application/x-www-form-urlencoded' -i http://localhost/WechatMedical/Admin/Login/Login --data 'username=admin&password=rootuser' | json
DELETE
curl -X DELETE -H 'Content-Type: application/x-www-form-urlencoded' -i http://localhost/KingsMap/3DBuilding --data'codeId=1'
上传文件
curl --form 'fileupload=@filename.txt' http://serverName/resource
保存和使用cookie
将cookie 保存到 cookie_file
curl -D cookie_file -X POST -H 'Content-Type: application/x-www-form-urlencoded' -i http://localhost/WechatMedical/Admin/Login/Login --data 'username=admin&password=rootuser' | json
让请求带上cookie
curl -b stored_cookies_in_file -X GET http://localhost/WechatMedical/Admin/UserManagement/getUserList?P=1&LIMIT=8 | json
领取专属 10元无门槛券
私享最新 技术干货