小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。今日为了测试一个问题,需要获取第三方的api数据,用到了curl,总结学习一下
基本概念
curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面 例如在windows命令行输入如下命令 curl www.baidu.com 返回内容如截图,这个是get请求,服务器返回的内容会在命令行输出。这种可以查看网页源码
详细用法
curl 默认是get方法,如果想用其他方法可以使用参数-X来指定其他请求 curl -X POST curl -X DELETE 这个参数和--request等价 curl --request post curl --request delete
想了解支持的所有的命令行参数,可以输入curl --help获取所有参数
通用格式:Usage: curl [options...] url
今天使用curl,使用的一些参数列表
--basic Use HTTP Basic Authentication 登录认证设置登录账号
--cert <certificate[:password]> Client certificate file and password 上传认证证书文件
-d, --data <data> HTTP POST data 需要传入的数据,是个json字符串
-H, --header <header/@file> Pass custom header(s) to server 设置接口的hearder信息
--key <key> Private key file name
-L, --location Follow redirects
-u, --user <user:password> Server user and password
另外,curl在Mac 命令行中运行顺畅, 在windows的命令行和power shell中会报一些意想不到的错误,可通过在git bash中运行解决