前言
科普界的老问题了。
大部分博客日志抄官方文档给的初始化样例,啥也不说。
看完除了会create,啥也不会了。
自食其力研究一下。
创建
略。
见document。
https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/mocking-with-examples/
或者随便找个中文博客。
推荐 https://zhuanlan.zhihu.com/p/371127186
但这篇文章的部分说法有误
比如文中称"Mock server只会采用最后创建的example"。 这是不对的。Mock Server会按规则匹配对应example。
创建之后,通过copy URL可以得到这个mock server的链接
默认作为公链
逻辑
postman逻辑结构有4层.
collection / folder / request / example
其中request是基本单位, 对应 http请求的寻址.
其实还有一个env,不过对这篇文章来说不重要。
1. request寻址
举例, 有一个http请求如下
axios.request({url:'/api'}
)
则需要对应地在postman中构建一个同地址的request.
{{url}}/api
值得注意的是, request中的地址部分, 前缀固定为"{{url}}"表示占位符.
此处输入 {{url}}/api 即可. 记得save.
最上面那个requestName = "New Request"可改可不改,就这样放着叫new request也无妨。
这个name是仅供postman使用者自己理解用的。
寻址是根据我们填写的url来的。
这样我们约定了一个地址为’/api’ 的接口。
接下来需要约定返回的数据。
2. example数据响应
request本身不能设置response data, 但example可以设置响应数据。
一个request下,可以添加多个example。
postman会根据http请求的不同,自动匹配对应的example,
然后返回该example中的response。
默认情况下,我们应该添加一个名为"Default"的example.
这个是缺省名字, 就像 index.html
, __init__.py
之类的一样.
这样写是符合规范的。
下面演示区别
我新建了一个New Request, 设置地址为
{{url}}/login
之后添加两个example, 分别命名为"Default" 和 “login”.
在"Default"中,我约定接口的方法是"POST", 返回值写"postlogin".
在"example2"中, 约定接口方法为"GET", 返回值 “getlogin”。
分别发送请求
axios.get({url:'/login'
}).then((res) => {console.log(res.data)})
axios.post({url:'/login'
}).then((res) => {console.log(res.data)})
可以看到非常良好地匹配上了两个example。
除了method以外, 还能匹配http response code等.
postman的匹配算法说明
https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/matching-algorithm/
3.数据动态响应
postman可以通过占位符传参。
最经典的param传参就是我们很常见的 问号后面跟key:value对的形式。
但是,经过测试这种键值对的url无法在mock server中被解析。(
mock server绑定动态变量的唯一方式,是通过slash或者backslash符号分割。
例如 /{{varName}}
这里需要注意的是,在response Body部分,
也是会经过mustache语法匹配,将"{{userID}}"部分替换为相应变量值的字符串 。
也可以使用反斜杠
/api/example\{{userID}}
这样写也能取到变量。
postman 官方似乎不推荐动态响应。
他们更希望你考虑多个不同的example取值可能性,提前写死所有query。
数据底层
先去https://web.postman.co/settings/me/api-keys
创建一个api-key.
然后新建一个request.
headers里面写上 x-api-key
url填
https://api.getpostman.com/collections/
请求之后,在response里找到自己的mock name,和对应的uid.
之后把uid,拼接到url后面
https://api.getpostman.com/collections/13803897-9e2bc2b2-2829-4224-8af8-449f53993b82
再请求一次
这一步返回的是当前mock server下的所有配置信息.
可以看到的是,每个request作为对象存放在 "item"数组中.
request下有一个属性"response"数组.
每个example作为对象存放在其中
在response中能为每个example找到唯一"id".
{"collection": {"info": {"_postman_id": "9e2bc2b2-2829-4224-8af8-449f53993b82","name": "假接口","schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"},"item": [{"name": "New Request","id": "4eb688f1-7e0b-4c82-8708-3523eadc2f25","protocolProfileBehavior": {"disableBodyPruning": true},"request": {"method": "POST","header": [],"body": {"mode": "raw","raw": ""},"url": {"raw": "{{url}}/login","host": ["{{url}}"],"path": ["login"]}},"response": [{"id": "428746aa-b59a-4785-a04b-be667a9528e7","name": "Default","originalRequest": {"method": "POST","header": [{"key": "Content-Type","name": "Content-Type","value": "application/javascript","type": "text"}],"body": {"mode": "raw","raw": "","options": {"raw": {"language": "javascript"}}},"url": {"raw": "{{url}}/login","host": ["{{url}}"],"path": ["login"],"query": [{"key": "name","value": "{{name}}","disabled": true},{"key": "password","value": "{{password}}","disabled": true}]}},"code": 200,"_postman_previewlanguage": null,"header": null,"cookie": [],"responseTime": null,"body": "{\"id\":\"11\",\"name\":\"abc\",\"token\":\"postlogin\"}"},{"id": "f012d8fb-9622-4544-b0cf-05b1fbfee298","name": "example2","originalRequest": {"method": "GET","header": [],"body": {"mode": "raw","raw": ""},"url": {"raw": "{{url}}/login","host": ["{{url}}"],"path": ["login"]}},"_postman_previewlanguage": null,"header": null,"cookie": [],"responseTime": null,"body": "{\"id\":\"22\",\"name\":\"abc\",\"token\":\"getlogin\"}"}]},{"name": "/get","id": "7b7d5f1f-75cf-453f-8309-b277bf715d9e","protocolProfileBehavior": {"disableBodyPruning": true},"request": {"method": "GET","header": [],"body": {"mode": "raw","raw": ""},"url": {"raw": "{{url}}/get","host": ["{{url}}"],"path": ["get"]}},"response": [{"id": "81501999-3d8a-44af-aa66-8e008bffb714","name": "Default","originalRequest": {"method": "GET","header": [],"url": {"raw": "{{url}}/get","host": ["{{url}}"],"path": ["get"]}},"code": 200,"_postman_previewlanguage": null,"header": null,"cookie": [],"responseTime": null,"body": "{ id=1,name='user2', token = 'abcdefg2'}"}]},{"name": "/user","id": "f8e05f82-91cc-41e6-9bdc-e914585d696e","protocolProfileBehavior": {"disableBodyPruning": true},"request": {"method": "GET","header": [],"url": {"raw": "{{url}}/user/{{userID}}","host": ["{{url}}"],"path": ["user","{{userID}}"],"query": [{"key": "id","value": null,"type": "text","disabled": true}]}},"response": [{"id": "af2743bd-2a3d-43cd-9ad5-775c11f67000","name": "Default","originalRequest": {"method": "GET","header": [],"url": {"raw": "{{url}}/user/{{userID}}","host": ["{{url}}"],"path": ["user","{{userID}}"],"query": [{"key": "id","value": null,"type": "text","disabled": true}]}},"_postman_previewlanguage": null,"header": null,"cookie": [],"responseTime": null,"body": "{\r\n \"id\": \"{{userID}}\",\r\n \"roleID\": \"01\" \r\n}"}]},{"name": "/role","id": "4d8b2c42-f3fd-4737-9329-6ed345021b8a","protocolProfileBehavior": {"disableBodyPruning": true},"request": {"method": "GET","header": [],"url": {"raw": "{{url}}/role/{{roleID}}","host": ["{{url}}"],"path": ["role","{{roleID}}"]}},"response": [{"id": "0891e08d-0aa0-4075-a63c-782c1f310fa5","name": "/role","originalRequest": {"method": "GET","header": [],"url": {"raw": "{{url}}/role/{{roleID}}","host": ["{{url}}"],"path": ["role","{{roleID}}"]}},"_postman_previewlanguage": null,"header": null,"cookie": [],"responseTime": null,"body": "{\r\n \"roleID\":\"{{roleID}}\",\r\n \"userMenus\":\"uerMenus\"\r\n}"}]}]}
}
在headers中设置
x-mock-response-name
或者
x-mock-response-id
即可对应到我们需要的example.
相当于绕过匹配规则,显式指定返回哪条example的response.
参考https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/mock-with-api/