cURL 是一个简单的 http 命令行工具。与最优秀的 Unix 工具一样,在设计之时,cURL 是个小型程序,功能十分专一,而且是故意为之,仅用于访问 http 服务器。(在 Linux 中,可以使用包管理器轻易安装,apt-get install curl
/yum install curl
)。
curl:默认会将下载文件输出到stdout$ curl
github api 是超媒体 api。从上述命令行的输出可以看出,其输出响应包含一个映射,列出了接下来可能会发起请求的地址,
code_search_url
键对应的 url 显然用于在 github 中搜索代码;-I/–head:只打印返回头信息,而不下载远程文件;
0. wget
-q(–quiet):turn off wget’s output;(对于 curl 则是 –silent)1. 查看网页源码
$ curl
(仅仅是查看)
<html><head><title>301 Moved Permanently</title></head><body bgcolor="white"><center><h1>301 Moved Permanently</h1></center><hr><center>nginx</center></body></html>
如果要将该网页保存下来,可以使用-o
参数,相当于 wget 命令了:
$ curl -o [filename]
2. 模拟GET/POST/PUT/DELETE/OPTIONS 请求
/entry/84875
常用参数:
-X/–request,后可跟:GET/POST/PUT/DELETE/OPTIONS
curl -X GET "/api/users"curl -X POST "/api/users"curl -X PUT "/api/users"curl -X DELETE "/api/users"
-H/–header,指定 request 的头部
curl -v -i -H "Content-Type: application/json" /users
-i/–include:显示 response 的 header
-d/–data:指定请求的参数;
curl -X POST -d "param1=value1¶m2=value2"