博客网站REST API设计
主要功能:
用户注册
Request
POST /api/v1/users
Input
name | type | description |
---|---|---|
username | string | name of the signing user |
password | string | password |
captchaID | string | ID of the captcha |
captchaCode | string | Code of the identifaction |
{
"username": "Zhangqzh"
"password": "Zhangqzh",
"captcha_id": "You have got a captcha ID",
"captcha_code": "Response",
"created_at": "2019-11-20T00:00:00Z"
}
Response
Status: 201 Created
Location: /api/v1/users
{
"id": 1
"user_name": "Zhangqzh",
"created_at": "2019-11-20T00:00:00Z"
}
用户登陆
Request
POST /api/v1/users/login
Input
name | type | description |
---|---|---|
username | string | name of the signing user |
password | string | password |
captchaID | string | ID of the captcha |
captchaCode | string | Code of the identifaction |
{
"username": "Zhangqzh"
"password": "Zhangqzh",
"captcha_id": "You have got a captcha ID",
"captcha_code": "Response",
"created_at": "2019-11-20T00:00:00Z"
"updated_at": "2019-11-20T01:00:00Z"
}
Response
Status: 201 OK
Location: /api/v1/users
{
"access——token": "token...",
"expires_in": 3600
}
Failed login limit
验证无效的凭据将返回401未经授权:
curl -i https://api.blog.com -u foo:bar
HTTP/1.1 401 Unauthorized
{
"message": "Bad credentials",
"documentation_url": "https://developer.blog.com/v3"
}
在短时间内检测到几个证书无效的请求后,API将暂时拒绝该用户的所有身份验证尝试(包括有效证书),403禁止
curl -i https://api.blog.com -u valid_username:valid_password
HTTP/1.1 403 Forbidden
{
"message": "Maximum number of login attempts exceeded. Please try again later.",
"documentation_url": "https://developer.blog.com/v3"
}
获取分类列表
Request
GET /api/v1/categories
Input
name | type | description |
---|---|---|
page | number | page number |
size | number | size of page |
Response
Status: 200 OK
Location: /api/v1/categories
{
"categories"[
{
"id": "0", //id
"node_id": "MDEwOlJlcG9zaXRvcnkxOTE0MzIzMjE=",//node id
"name": "Go语言学习", //分类名称
"count": 10 // 文章数
}]
}
创建分类列表
Request
POST /api/v1/categories
Input
name | type | description |
---|---|---|
name | strinf | name of category |
{
“name”: “3D unity”
}
Response
Status: 201 Created
Location: /api/v1/categories
{
"id": 1,
"name": "3D unity"
}
注意:必须添加权限验证,否则返回401 Unauthorized
更新分类列表
Request
PUT /api/v1/categories/:category_id
Input
name | type | description |
---|---|---|
category_name | string | name of category |
{
"name": "3D unity"
}
Response
Status: 200 OK
Location: /api/v1/categories/0
{
"id": 1,
"name": "3D unity"
"message": "update from ",
"content": "Y3JlYXRlIGZpbGUgZnJvbSBJTlNPTU5JQQoKSXQncyB1cGRhdGVkISEhCgpJdCdzIHVwZGF0ZWQgYWdhaW4hIQ==",
}
注意:必须添加权限验证,否则返回401 Unauthorized
删除分类
Request
DELETE /api/v1/categories:category_id
Input
name | type | description |
---|---|---|
category_name | string | name of category |
{
"name": "3D unity"
}
Response
Status: 204 No Content
Location: /api/v1/categories/0
{
"message": "delete a category",
}
注意:必须添加权限验证,否则返回401 Unauthorized
获取分类内的文章
Request
GET /api/v1/categories/:category_id
Input
name | type | description |
---|---|---|
page | number | page number |
size | number | size of page |
Response
Status: 200 OK
Location: /api/v1/categories/0
{
"category": "Go 语言学习",
"articles": [
{
"id": 0,
"title": "源码学习-net/http",
"created_at": "2019-11-11T00:00:00Z"
},
{
"id": 1,
"title": "阅读《Golang web应用开发》",
"created_at": "2019-11-05T00:00:00Z"
}
]
...
}
获取某个文章
Request
GET /api/v1/articles/:article_id
Response
Status: 200 OK
Location: /api/v1/articles/0
{
"id": 0,
"category_id": 0,
"category_name": "Go 语言学习",
"article_title": "源码学习-net/http",
"article_content": "*****"
}
创建新文章
Request
POST /api/v1/articles
Input
name | type | description |
---|---|---|
catagory_id | number | id of category |
article_title | string | title of article |
content | string | content of article |
{
"category_id": 0,
"article_id": 2,
"title": "博客网站REST API设计",
"content": "*****"
}
Response
Status: 201 Created
Location: /api/v1/articles
{
"categoryId": 0,
"article_id": 2,
"title": "博客网站REST API设计",
"content": "*****"
"size" : "*****"
"created_at":"2019-11-20T00:00:00Z"
"url": "https://api.blog.com/repos/go-"
}
注意:1. 必须添加权限验证,否则返回401 Unauthorized
2. 数据传送格式选择JSON 3. 文件内容必须是把文件整体转为Base64字符串再存到JSON变量中
更新文章
Request
PUT /api/v1/articles/:article_id
Input
name | type | description |
---|---|---|
catagory_id | number | id of category |
article_title | string | title of article |
content | string | content of article |
{
"category_id": 0,
"article_id": 2,
"title": "博客网站REST API设计",
"content": "*****"
}
Response
Status: 200 OK
Location: /api/v1/articles/3
{
"categoryId": 0,
"article_id": 2,
"title": "博客网站REST API设计",
"content": "*****"
"size" : "*****"
"created_at":"2019-11-20T00:00:00Z"
"updated_at":"2019-11-20T14:00:00Z"
"url": "https://api.blog.com/repos/go-"
}
注意:必须添加权限验证,否则返回401 Unauthorized
对文件格式有要求,如果没有按照格式把上传时的json必填项输入,则会出现422错误信息:
{
"message": "Invalid request...",
"url": "https://api.blog.com/repos/go-"
}
删除文章
Request
DELETE /api/v1/articles/:article_id
{
"message": "delete a file",
}
Response
Status: 204 No Content
Location: /api/v1/articles/1
{
"content": null,
"url": "https://api.blog.com/repos/go-"
}
获取文章的评论
Request
GET /api/v1/articles/:article_id/comments
Input
name | type | description |
---|---|---|
page | number | page number |
size | number | size of page |
Response
Status: 200 OK
Location: /api/v1/articles/
{
"id": "0",
"user": {
"id": 0,
"username": "zz",
},
"content": "*****",
"created_at": "2019-11-20T00:00:00Z"
},
添加评论
Request
POST /api/v1/articles/:article_id/comments
Input
name | type | description |
---|---|---|
content | string | content |
{
"content": "*****"
}
Response
Status: 202 Accepted
Location: /api/v1/articles/0/comments
{
"id": "0",
"article_title": "博客网站REST API设计"
"content": "*****",
"createdAt": "2019-11-19T00:00:00Z"
}