目录
mock服务
使用前准备
环境选择必须是Mock环境
非mock环境 mock服务是不起作用的。环境设置如下:路径:在小眼睛左边
相对地址自定义
Mock响应数据编辑
数据编辑框 & 预览数据框比如数据如下填写:
{ "id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB", "name": "李四", "phone": "" }固定基本数据使用
{ code:1000, msg:"sucess" ,data:{ "id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB", "name": "李四", "phone": "" } }随机动态数据
{ "code": "0", "data": { "list|10": [{ "name": "@name", "age": "@integer(2)" }], "url": "" }, "desc": "成功" }{ "code": "0", "data": { "list": [ { "name": "Susan Williams", "age": 17013 }, { "name": "Cynthia Martinez", "age": 79682 }, { "name": "Joseph Rodriguez", "age": 19320 }, { "name": "Susan Perez", "age": 33828 }, { "name": "Scott White", "age": 06224 }, { "name": "David Allen", "age": 54853 }, { "name": "Jennifer Brown", "age": 7008 }, { "name": "Margaret Anderson", "age": 59736 }, { "name": "Brenda Lee", "age": 2656 }, { "name": "Brian Jackson", "age": 63432 } ], "url": "" }, "desc": "成功" }实际产生的mock数据。刚开始看到时候很惊艳一把哈!
RESTFUL逻辑动态Mock数据响应
{ "code": "0000", "data": { "verifySuccess": function() { let body = _req.body; console.log(body); return body.phone === && body.pwd === ; }, "userInfo": function() { let body = _req.body; if (body.phone === && body.pwd === ) { return Mock.mock({ "id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB", "name": "李四", "phone": "" }); } 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"));生成代码
路径:接口界面–》生成代码
生成代码界面如下图:
包含的开发语言有:
cURLJavaPHPGoPythonNodeRRustStrestDartMATLABElixirAnsibleBrowserJsonStringC#列举几个常用的开发语言
c#
// Generated by ApiPost: using (var httpClient = new HttpClient()) { using (var request = new HttpRequestMessage(new HttpMethod("Get"), "")) { request.Headers.TryAddWithoutValidation("User-Agent", "Apipost client Runtime/+"); request.Headers.TryAddWithoutValidation("Content-Type", "application/json"); var response = await httpClient.SendAsync(request); } }Java
// Generated by ApiPost: Request request = Request.Get(""); String body = ""; request.bodyString(body,ContentType.APPLICATION_JSON); request.setHeader("User-Agent", "Apipost client Runtime/+"); 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/+, Content-Type: application/json, } response = requests.post(, headers=headers)