官方文档
可参考B1u3Buf4的博客简单使用。
可以直接再浏览器上输入:
https://api.github.com/users/chenjb
users/后面+自己的github用户名,获取用户信息
{
"login": "chenjb",
"id": 4031969,
"node_id": "MDQ6VXNlcjQwMzE5Njk=",
"avatar_url": "https://avatars1.githubusercontent.com/u/4031969?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/chenjb",
"html_url": "https://github.com/chenjb",
"followers_url": "https://api.github.com/users/chenjb/followers",
"following_url": "https://api.github.com/users/chenjb/following{/other_user}",
"gists_url": "https://api.github.com/users/chenjb/gists{/gist_id}",
"starred_url": "https://api.github.com/users/chenjb/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/chenjb/subscriptions",
"organizations_url": "https://api.github.com/users/chenjb/orgs",
"repos_url": "https://api.github.com/users/chenjb/repos",
"events_url": "https://api.github.com/users/chenjb/events{/privacy}",
"received_events_url": "https://api.github.com/users/chenjb/received_events",
"type": "User",
"site_admin": false,
"name": null,
"company": null,
"blog": "",
"location": null,
"email": null,
"hireable": null,
"bio": null,
"public_repos": 6,
"public_gists": 0,
"followers": 2,
"following": 5,
"created_at": "2013-04-02T01:41:12Z",
"updated_at": "2019-05-20T05:53:00Z"
}
https://adventure-works.com/orders/1
{"orderId":1,"orderValue":99.90,"productId":1,"quantity":1}
{
"orderID":3,
"productID":2,
"quantity":4,
"orderValue":16.60,
"links": [
{"rel":"product","href":"https://adventure-works.com/customers/3", "action":"GET" },
{"rel":"product","href":"https://adventure-works.com/customers/3", "action":"PUT" }
]
}
2008 年,Leonard Richardson 提议对 Web API 使用以下成熟度模型:
级别 0:定义一个 URI,所有操作是对此 URI 发出的 POST 请求。
级别 1:为各个资源单独创建 URI。
级别 2:使用 HTTP 方法来定义对资源执行的操作。
级别 3:使用超媒体(HATEOAS,如下所述)。
根据 Fielding 的定义,级别 3 对应于某个真正的 RESTful API。 在实践中,许多发布的 Web API 大致处于级别 2。
Verb | 描述 |
---|---|
HEAD | 只获取某个资源的头部信息 |
GET(SELECT) | 从服务器取出资源 |
POST(CREATE) | 在服务器新建一个资源 |
PUT(UPDATE) | 在服务器更新资源(客户端提供改变后的完整资源) |
PATCH(UPDATE) | 在服务器更新资源(客户端提供改变的属性) |
DELETE(DELETE) | 从服务器删除资源 |
根据上节课所学内容可以进行相关的设计
curl -u username -p password https://api.exampleBlog.com
成功登陆的返回状态码是2xx;失败的返回码是4xx,有以下几种情况:
报错 | 错误原因 |
---|---|
400 Bad request | 指令错误 |
401 Unauthorized | 密码错误 |
403 Forbidden | 访问过于频繁 |
404 Not found | 账户不存在 |
curl -a https://api.exampleBlog.com/chenjb58/articles
curl -c 'my comments' https://api.exampleBlog.com/chenjb58/article/1/comments
curl -d -i https://api.exampleBlog.com/chenjb58/delete/article?id=1