博客网站REST API设计

博客网站REST API设计

主要功能:

用户注册

Request

POST /api/v1/users

Input

nametypedescription
usernamestringname of the signing user
passwordstringpassword
captchaIDstringID of the captcha
captchaCodestringCode 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

nametypedescription
usernamestringname of the signing user
passwordstringpassword
captchaIDstringID of the captcha
captchaCodestringCode 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

nametypedescription
pagenumberpage number
sizenumbersize 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

nametypedescription
namestrinfname 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

nametypedescription
category_namestringname 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

nametypedescription
category_namestringname 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

nametypedescription
pagenumberpage number
sizenumbersize 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

nametypedescription
catagory_idnumberid of category
article_titlestringtitle of article
contentstringcontent 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

nametypedescription
catagory_idnumberid of category
article_titlestringtitle of article
contentstringcontent 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

nametypedescription
pagenumberpage number
sizenumbersize 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

nametypedescription
contentstringcontent
{
    "content": "*****"
}
Response
Status: 202 Accepted
Location: /api/v1/articles/0/comments
{
    "id": "0",
    "article_title": "博客网站REST API设计"
    "content": "*****",
    "createdAt": "2019-11-19T00:00:00Z"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值