mock服务
使用前准备
环境选择必须是Mock环境
非mock环境 mock服务是不起作用的。 环境设置如下: 路径:在小眼睛左边
编辑切换为居中
添加图片注释,不超过 140 字(可选)
相对地址自定义
Mock响应数据编辑
数据编辑框 & 预览数据框 比如数据如下填写:
{
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
}
固定基本数据使用
{
code:1000,
msg:"sucess"
,data:{
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
}
}
随机动态数据
{
"code": "0",
"data": {
"list|10": [{
"name": "@name",
"age": "@integer(2)"
}],
"url": "https://echo.apipost.cn"
},
"desc": "成功"
}
{
"code": "0",
"data": {
"list": [
{
"name": "Susan Williams",
"age": 4202047804417013
},
{
"name": "Cynthia Martinez",
"age": 7836460292479682
},
{
"name": "Joseph Rodriguez",
"age": 7904378098119320
},
{
"name": "Susan Perez",
"age": 2839392309833828
},
{
"name": "Scott White",
"age": 8529217801206224
},
{
"name": "David Allen",
"age": 3507736869454853
},
{
"name": "Jennifer Brown",
"age": 186237858137008
},
{
"name": "Margaret Anderson",
"age": 7679312547559736
},
{
"name": "Brenda Lee",
"age": 105486912822656
},
{
"name": "Brian Jackson",
"age": 1856742232963432
}
],
"url": "https://echo.apipost.cn"
},
"desc": "成功"
}
实际产生的mock数据。刚开始看到时候很惊艳一把哈!
RESTFUL逻辑动态Mock数据响应
{
"code": "0000",
"data": {
"verifySuccess": function() {
let body = _req.body;
console.log(body);
return body.phone === '13718425555' && body.pwd === '123456';
},
"userInfo": function() {
let body = _req.body;
if (body.phone === '13718425555' && body.pwd === '123456') {
return Mock.mock({
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
});
} else {
return null;
}
},
},
"desc": "成功"
}
使用Mock服务的时候同时使用断言
断言定义如下:
apt.assert('response.raw.responseTime>=50');
apt.assert('response.raw.type=="json"');
apt.assert('response.json.hasOwnProperty("code")');
apt.assert('response.json.hasOwnProperty("msg")');
apt.assert('response.json.hasOwnProperty("data")');
编辑切换为居中
添加图片注释,不超过 140 字(可选)
生成代码
路径:接口界面–》生成代码
生成代码界面如下图:
包含的开发语言有:
- cURL
- Java
- PHP
- Go
- Python
- Node
- R
- Rust
- Strest
- Dart
- MATLAB
- Elixir
- Ansible
- Browser
- JsonString
- C#
列举几个常用的开发语言
c#
// Generated by ApiPost: https://www.apipost.cn/
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("Get"), "https://console-mock.apipost.cn/app/mock/project/e506875e-055f-452a-c781-e00e05f2f9f9/getUserInfo"))
{
request.Headers.TryAddWithoutValidation("User-Agent", "Apipost client Runtime/+https://www.apipost.cn/");
request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
var response = await httpClient.SendAsync(request);
}
}
Java
// Generated by ApiPost: https://www.apipost.cn/
Request request = Request.Get("https://console-mock.apipost.cn/app/mock/project/e506875e-055f-452a-c781-e00e05f2f9f9/getUserInfo");
String body = "'";
request.bodyString(body,ContentType.APPLICATION_JSON);
request.setHeader("User-Agent", "Apipost client Runtime/+https://www.apipost.cn/");
request.setHeader("Content-Type", "application/json");
HttpResponse httpResponse = request.execute().returnResponse();
System.out.println(httpResponse.getStatusLine());
if (httpResponse.getEntity() != null) {
String html = EntityUtils.toString(httpResponse.getEntity());
System.out.println(html);
}
Python:
import requests
headers = {
'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
'Content-Type': 'application/json',
}
response = requests.post('https://console-mock.apipost.cn/app/mock/project/e506875e-055f-452a-c781-e00e05f2f9f9/getUserInfo', headers=headers)
好用的Api工具推荐
历时一年半多开发终于smartApi-v1.0.0版本在2023-09-15晚十点正式上线 smartApi是一款对标国外的postman的api调试开发工具,由于开发人力就作者一个所以人力有限,因此v1.0.0版本功能进行精简,大功能项有:
- api参数填写
- api请求响应数据展示
- PDF形式的分享文档
- Mock本地化解决方案
- api列表数据本地化处理
- 再加上UI方面的打磨 本地运行,性能优越,高效,快捷
下面是一段smartApi使用介绍: