curl学习笔记

这个是GitHub实践的读书笔记

curl的学习

cURL适合发起一次性请求

安装

yum install curl

发起第一次请求

curl https://api.github.com

显示结果

{
  "current_user_url": "https://api.github.com/user",
  "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
  "authorizations_url": "https://api.github.com/authorizations",
  "code_search_url": "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}",
  "commit_search_url": "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}",
  "emails_url": "https://api.github.com/user/emails",
  "emojis_url": "https://api.github.com/emojis",
  "events_url": "https://api.github.com/events",
  "feeds_url": "https://api.github.com/feeds",
  "followers_url": "https://api.github.com/user/followers",
  "following_url": "https://api.github.com/user/following{/target}",
  "gists_url": "https://api.github.com/gists{/gist_id}",
  "hub_url": "https://api.github.com/hub",
  "issue_search_url": "https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}",
  "issues_url": "https://api.github.com/issues",
  "keys_url": "https://api.github.com/user/keys",
  "label_search_url": "https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}",
  "notifications_url": "https://api.github.com/notifications",
  "organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}",
  "organization_url": "https://api.github.com/orgs/{org}",
  "public_gists_url": "https://api.github.com/gists/public",
  "rate_limit_url": "https://api.github.com/rate_limit",
  "repository_url": "https://api.github.com/repos/{owner}/{repo}",
  "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}",
  "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}",
  "starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
  "starred_gists_url": "https://api.github.com/gists/starred",
  "team_url": "https://api.github.com/teams",
  "user_url": "https://api.github.com/users/{user}",
  "user_organizations_url": "https://api.github.com/user/orgs",
  "user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}",
  "user_search_url": "https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"
}

上述是GitHub API的显示

curl 在我的学习过程中听过很多次,此为第一次实践,不自觉有点兴奋.

响应中有很多包含指向符数信心的URL URL中也包含参数,相应的格式为JSON

尝试一言的API

curl https://v1.hitokoto.cn/

返回信息

{
  "id": 4627,
  "hitokoto": "漂亮姑娘千千万,功名万里惟一人!",
  "type": "e",
  "from": "春风词",
  "creator": "Sariay",
  "created_at": "1567970414"
}

JSON格式

json 是一种轻量级的数据交换格式,与XML YAML相争

JSON 只支持双引号,不支持双引号

JSON优势

  1. 易于阅读(人类阅读)
  2. 只需要小规模修改便可以在JavaScript中使用

在命令行解析JSON

工具介绍 jq(通过管道 | 把JSON传给jq后,可以使用过滤器轻易提取JSON片段)

下载安装jq

网址: https://stedolan.github.io/jq/download/

哭了,官方介绍中没有CentOS,此次试试直接下载安装(虽然我觉得也有yum 的package)

From source on Linux, OS X, Cygwin, and other POSIX-like operating systems

git clone https://github.com/stedolan/jq.git
cd jq
autoreconf -i
./configure --disable-maintainer-mode
make
sudo make install

这个要有相应的依赖
To build it from a git clone, you’ll need to install a few packages first:
- GCC
- Make
- Autotools

有点麻烦,直接用yum install 解决了

>  **yum intall 真香**

解析

使用管道实现

curl https://api.github.com | jq '.current_user_url' 

[root@VM_14_21_centos jq]# curl https://api.github.com | jq '.current_user_url'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2283  100  2283    0     0   2950      0 --:--:-- --:--:-- --:--:--  2949
"https://api.github.com/user"

上述中将传输信息打印出来了
curl -s 采用静默传输 不打印 传输信息
demo

curl -s https://api.github.com/users/tcpgnl

显示的是GitHub的个人信息

{
  "login": "TCPGNL",
  "id": 51265740,
  "node_id": "MDQ6VXNlcjUxMjY1NzQw",
  "avatar_url": "https://avatars3.githubusercontent.com/u/51265740?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/TCPGNL",
  "html_url": "https://github.com/TCPGNL",
  "followers_url": "https://api.github.com/users/TCPGNL/followers",
  "following_url": "https://api.github.com/users/TCPGNL/following{/other_user}",
  "gists_url": "https://api.github.com/users/TCPGNL/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/TCPGNL/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/TCPGNL/subscriptions",
  "organizations_url": "https://api.github.com/users/TCPGNL/orgs",
  "repos_url": "https://api.github.com/users/TCPGNL/repos",
  "events_url": "https://api.github.com/users/TCPGNL/events{/privacy}",
  "received_events_url": "https://api.github.com/users/TCPGNL/received_events",
  "type": "User",
  "site_admin": false,
  "name": "TCPGNL",
  "company": null,
  "blog": "https://www.TCPGNL.COM",
  "location": null,
  "email": null,
  "hireable": null,
  "bio": null,
  "public_repos": 7,
  "public_gists": 0,
  "followers": 0,
  "following": 3,
  "created_at": "2019-06-01T13:30:51Z",
  "updated_at": "2020-01-03T04:56:02Z"
}

可以显示我的所有GitHub的信息
使用jq 获取我的网站信息

curl -s https://api.github.com/users/tcpgnl | jq ".blog"

由此显示BLOG

curl调试

参数

-i 请求打印头部

-v 请求打印请求和相应头部(>后接请求数据 <后接响应数据>

重要Tag

jq 不可以解析头部,注意

在请求头部有相应的API的请求限制的信息

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58

curl 身份验证

命令

curl -u 参数 网站 
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注