RESTful API使用
当前端在开发时,会遇到使用API接口获取数据的情况,这是如果后端接口没有写好,通过Data Assistant 模拟RESTful接口,可以帮助您进行快速测试。
下面将通过一个简单的示例帮助开发者使用 Data Assistant 的 RESTful API 服务。假设我们有一张users表,该表定义了以下字段:
每一行的数据格式如下:
{
"id": 790,
"name": "Crane Patti",
"age": 801,
"friends": {
"id": 666,
"name": "Mccarthy Eduardo",
"age": 579
}
}
定义好表字段之后,在服务器配置页设置服务器的运行地址为localhost,端口为3000,接口响应延迟为0ms,表的id字段名。
接着点击Start Server按钮即可开启服务,开启服务器后,将对外暴露RESTFul风格的接口,接口规范如下:
-
向表中添加新数据
POST http://localhost:3000/users/
Request Body (JSON格式)
{ "id": 1, "name": "Crane Patti", "age": 45, "friends": { "id": 2, "name": "Mccarthy Eduardo", "age": 579 } }
-
删除表中的一行数据
DELETE http://localhost:3000/users/1
Response Body
{ "id": 1, "name": "Crane Patti", "age": 45, "friends": { "id": 2, "name": "Mccarthy Eduardo", "age": 579 } }
-
修改表中一行数据:
PUT http://localhost:3000/users/1
Request Body
{ "id": 1, "name": "hello world", "age": 45, "friends": { "id": 2, "name": "Mccarthy Eduardo", "age": 579 } }
-
查询全部数据
GET http://localhost:3000/users
Response Body
[ { "id": 121, "name": "cxhrumpo", "age": 14, "friend": { "id": 121, "name": "芦婉丽", "age": 28 } }, { "id": 122, "name": "ogfbtudp", "age": 93, "friend": { "id": 122, "name": "张涛", "age": 5 } }, { "id": 123, "name": "kktknzfr", "age": 38, "friend": { "id": 123, "name": "淳于宏", "age": 88 } } ]
-
根据主键查询
GET http://localhost:3000/users/1
Response Body
{ "id": 1, "name": "hello world", "age": 45, "friends": { "id": 2, "name": "Mccarthy Eduardo", "age": 579 } }
-
分页查询
GET http://localhost:3000/users?_page=0&_limit=3
Response Body
[ { "id": 121, "name": "cxhrumpo", "age": 14, "friend": { "id": 121, "name": "芦婉丽", "age": 28 } }, { "id": 122, "name": "ogfbtudp", "age": 93, "friend": { "id": 122, "name": "张涛", "age": 5 } }, { "id": 123, "name": "kktknzfr", "age": 38, "friend": { "id": 123, "name": "淳于宏", "age": 88 } } ]