curl 是利用 URL 语法在命令行方式下工作的开源文件传输工具。它的名字就是客户端 client 的 URL 工具的意思。
Mac 和 Linux 自带 curl,Windows 需要下载安装。git shell 自带 curl。
基本用法:
curl baidu.com
-i 返回 HTTP 头:
curl -i https://baidu.com
-H 设置请求的 HTTP 头:
curl -H 'Content-Type: appication/json' https://baidu.com
注意:URL 通常用双引号防止转义,例如在 linux 中使用 & 会使进程在后台运行,必须对 & 进行转义,加反斜杠的方式比较麻烦,故使用双引号模式最方便。
curl "baidu.com?a=1&b=2&c=3"
-d 发起 POST 请求:
curl -d 'login=test&password=123456'-X POST https://baidu.com/login
--data-urlencode 参数等同于 -d,区别是会自动将发送的数据进行 URL 编码:
curl --data-urlencode 'msg=hello' https://baidu.com
-G 发起 GET 请求,不写默认 POST 请求:
curl -G --data-urlencode "a=1&b=2" https://baidu.com