文章目录
作业内容
模仿github API,用markdown编写设计一个博客网站的部分REST API。
REST
REST(Representational State Transfer,表现层状态装换)是Roy Thomas Fielding博士于2000年在它的博士论文中提出的一种万维网软件架构风格(而不是标准),目的是便于不同软件/程序在网络中互相传递信息(详见维基百科)。
REST API设计
假设博客根地址为https://blog.com。
架构概述
- 所有API访问使用HTTP协议,并通过https://api.blog.com进行访问。
- 所有数据以JSON形式发送接收,所有时间戳以ISO 8601格式(YYYY-MM-DDTHH:MM:SSZ)返回。
- 请求成功时响应状态返回2xx,失败时返回4xx或者5xx。
- HTTP常用动词:PUT,DELETE,GET,POST,PATCH。
部分API设计
使用curl发送请求
错误信息
401:验证无效的凭据将返回401
HTTP/1.1 401 Unauthorized
{
"message": "Bad credentials",
"documentation_url": "https://developer.blog.com/v3"
}
403:假如短时间内检测到几个证书无效的请求后,API将暂时拒绝该用户的所有身份验证尝试(包括有效证书)。
HTTP/1.1 403 Forbidden
{
"message": "Maximum number of login attempts exceeded. Please try again later.",
"documentation_url": "https://developer.blog.com/v3"
}
404:当未经授权的用户尝试访问私有数据时将返回404 Not Found
用户登录
用户登录有两种方式
curl -u username https://blog.com
-u 后的username为登录的用户名,使用以上命令后会要求输入对应的密码,密码正确即可登录。还可以使用以下带有密码的命令直接登录
curl -i https://blog.com -u username:password
假如使用curl -u fsq https://blog.com 登录成功,则返回
HTTP/1.1 200 OK
{
"message": "User login success",
"username": "fsq"
}
新建博客
请求:
POST /:username/articles/publish
命令参数
-d:JSON形式的data参数内容,包括文章内容及标题
{
"title":"title of the blog"
"private":true/false,
"description":"description of the blog"
"content":"content of the blog",
}
命令示例:
curl -i 'https://api.blog.com/fsq/articles/publish' -d
'{ "title":"title...",
"private":false,
"description":"description ...",
"content":"content...."
}'
成功响应:
HTTP/1.1 201 Created
{
"isPublished":true,
"article":{
"articleID": 01,
"title": "title",
"author": {
"name": "fsq",
"id": 1236789,
"type": "User"
},
"private": false,
"description": "...",
"reading number": 0,
"created_at": "2019-11-20T00:19:49Z",
"words": 800,
"url": "article url"
"content":"article content...."
},
}
修改博客
请求:
更新博客与发布博客类似,但是命令由POST改为PUT,
PUT /:username/articles/update
命令参数:
-d:JSON形式的data参数内容,与发布文章类似,但是增加了文章ID,命令根据ID修改文章
{
"articleID":"ID of the blog",
"title":"title of the blog"
"private":true/false,
"description":"description of the blog"
"content":"content of the blog",
}
命令示例:
curl -i 'https://api.blog.com/fsq/articles/publish' -d
'{
"articleID":"ID of the blog",
"title":"title of the blog"
"private":true/false,
"description":"description of the blog"
"content":"content of the blog",
}'
成功响应:
HTTP/1.1 200 OK
{
"isUpdate":true,
"article":{
"articleID": 01,
"title": "title",
"author": {
"name": "fsq",
"id": 1236789,
"type": "User"
},
"private": false,
"description": "...",
"reading number": 1,
"created_at": "2019-11-20T19:48:00Z",
"update_at": "2019-11-20T19:55:00Z"
"words": 900,
"url": "article url"
"content":"article content...."
},
}
删除博客
根据博客id删除博客
请求:
DELETE /:username/articles/delete
命令参数:
name | type | description |
id | int | id of the article to delete |
命令示例:
curl -i -u 'https://api.blog.com/fsq/articles/delete?articles_id=01'
响应:
HTTP/1.1 200 OK
{
"isDeleted":true,
"id": 01
}
查看博客
根据博客id查看博客内容。
请求:
GET /:username/articles
命令参数:
name | type | description |
id | int | id of the article to look up |
命令示例:
curl -i -u 'https://api.blog.com/fsq/articles?articles_id=01'
成功响应:
HTTP/1.1 200 OK
{
"article":{
"articleID": 01,
"title": "title",
"author": {
"name": "fsq",
"id": 1236789,
"type": "User"
},
"private": false,
"description": "...",
"reading number": 1,
"created_at": "2019-11-20T19:48:00Z",
"update_at": "2019-11-20T19:55:00Z"
"words": 900,
"url": "article url"
"content":"article content...."
},
}
查看评论
根据博客ID查看该博客的评论。
请求:
GET /:username/articles/comments
命令参数:
name | type | description |
id | int | id of the article to look for the comments |
命令示例:
curl -i -u 'https://api.blog.com/fsq/articles/comments?articles_id=01'
成功响应:
{
"id": 01,
"author": "fsq",
"url": "article url",
items:[{
"comment1": {
"username":"user2",
"user_id": 2,
"type": "User",
"content": "comment content",
"created_at": "2019-12-01T00:00:00Z"
},
"comment2": {
"username":"user3",
"user_id": 3,
"type": "User",
"content": "comment content",
"created_at": "2019-12-01T01:00:00Z"
},
... ...
}],
}
发表评论
根据博客id将评论发表至对应博客
请求:
POST /:username/articles/comment
命令参数:
博客id与评论内容content
命令示例:
curl -i -u 'https://api.blog.com/fsq/articles/comments?articles_id=01' -d {"content": "comment content"}
响应:
{
"id": 01,
"author": "author_name",
"content":xx,
"user":{
"username": "fsq",
"id": 12345678,
"type": "User"
}
"created_at": "2019-12-01T05:00:00Z",
}
来源:CSDN
作者:Excef
链接:https://blog.csdn.net/Excef/article/details/103189122