1)通信协议:API与用户的通信协议,总是使用HTTPs协议。
2)数据格式:统一采用JSON。
3)URI格式统一:同一作者发布另一篇博文,则仅仅改变URI后面的部分,URI的前面部分不应改变。
4)状态码:统一根据HTTP协议:
默认情况下,所有https://api.weblog.com接收当前版本(V1)的REST API的请求。
Accept:application/vnd.weblog.v1+json
注:所有的时间戳都以ISO 8601格式返回:
YYYY-MM-DDTHH:MM:SSZ
curl -u username -p password https://weblog.com/v1
登录成功:返回状态码为:2xx
登录失败:返回状态码为:4xx
注:以下API均在登录的情况下使用。
GET /username
curl -u https://weblog.com/v1/huangshim23
响应:
{
Status:200 OK
----------------
"username":huangshim23.
"age":20,
"gender": man,
"fans":null,
"coin":0,
"interestring field":Computing Service,3D,
"created_at":2017-10-01T14:20:35+08:00
}
PUT /user
data //修改用户的参数
{
"age": 21,
...
}
curl -u -d {
"age": 21, ...} https://weblog.com/v1/huangshim23
响应:
{
Status:200 OK
----------------
"username":huangshim23.
"age":21,
"gender": man,
"fans":null,
"coin":0,
"interestring field":Computing Service,3D,
"created_at":2017-10-01T14:20:35+08:00
}
1)查看用户发表的所有文章
GET /username/articles
curl -a https://weblog.com/v1/huangshim23/articles
响应:
{
"username":"huangshim23"
"total_num":20,
"articles":[
{
"title":"title1",
"id": 1,
"href":"https://weblog.com/v1/huangshim23/articles/1",
"action":"GET",
"status":"original"
"created_time": "2010-11-11T17:31:50Z",
"updated_time": "2014-11-11T17:58:47Z",
"words": 3000,
"visits": 1000
},
{
"title":"title2",
"id": 1,
"href":"https://weblog.com/v1/huangshim23/articles/2",
"action":"GET",
"status":"reproduced"
"created_time": "2016-11-21T17:31:50Z",
"updated_time": "2018-08-11T17:58:47Z",
"words": 5000,
"visits": 10
},
...
]
}
2)查看特定的文章
GET /username/articles/{
id}
GET /username/articles/{
title}
curl -u https://weblog.com/v1/huangshim23/articles/1
curl -u https://weblog.com/v1/huangshim23/articles/hello-world
响应:
{
"username": huangshim23,
"title":"hello-world",
"id": 1,
"href":"https://weblog.com/v1/huangshim23/articles/1",
"action":"GET",
"status":"original"
"created_time": "2016-11-21T17:31:50Z",
"updated_time": "2018-08-11T17:58:47Z",
"words": 3000,
"visits": 1000
}
POST /user/articles
data //发布文章的数据参数
{
"title":
"content":
"description":
"visibility":
"status":
}
curl -u -p -d '{"title":"xxx","content":"xxx","description":"xxxx","visibility":true, "status": "orginal"}' https://weblog.com/v1/huangshim23/articles
响应:
Status:200 OK
--------------------------
"isPublished":true ,
"article":{
"id": 3,
"title": "articleTitle",
"owner": {
"name": "haungshim23",
"id": 123,
"url": "https://weblog.com/v1/huangshim23",
"type": "User"
},
"article_url": "https://weblog.com/v1/huangshim23/articles/3",
"private": false,
"description": "...",
"reading number": 1,
"created_at": "2016-11-11T17:31:50Z",
"updated_at": "2016-11-11T17:31:50Z",
"words": 1502,
"language": "Chinese",
"content":"article contents...."
}
}
DELETE /user/articles/id
curl -d -i https://weblog.com/v1/huangshim23/articles/1
响应:
Status:200 OK
--------------------------
{
"successed": "true",
"article_id": "1",
"created_at":"2019-09-01T11:30:50Z",
"deleted_at": "2019-11-20T10:30:50Z"
}
GET /user/articles/id/comments
curl -u https://weblog.com/v1/huangshim23/articles/1/comments
响应:
{
Status:200 OK
--------------------------
"comments":[
{
"userName":"user1",
"comment": "...",
"comment_time":"2019-11-11T10:30:50Z",
"stars": 100
},
{
"userName":"user2",
"comment": "...",
"comment_time":"2019-11-21T12:30:20Z",
"stars": 1
},
...
]
}
POST /user/articles/id/comments
curl -c '评论内容' https://weblog.com/v1/huangshim23/articles/1/comments
响应:
{
"comment_status": "succeeded",
"comment": "评论内容",
"userName": "huangshim23",
"comment_time": "2019-11-21T12:30:20Z"
}