mockoon造随机数据
参考网址:https://mockoon.com/tutorials/generate-mock-json-data/
{{ helperName }} | 实例 | 说明 |
---|---|---|
{{@index}} | 1 | 随机数字索引 |
{{faker ‘datatype.uuid’}} | a1b18846-9ef6-45ee-bab1-9e7135c8bca0 | 生成随机uuid |
{{faker ‘datatype.boolean’}} | false | 生成随机布尔值 |
{{faker ‘datatype.number’ 50}} | 30 | 随机生成50以内的数字 |
{{faker ‘image.nature’}} | http://placeimg.com/640/480/nature | 图片网址 |
{{faker ‘commerce.color’}} | olive | 随机生成颜色 |
{{faker ‘commerce.price’}} | 718.00 | 随机生成价格 |
从请求中获取数据,传入响应体中
请求类型 | 提取目标 | 说明 |
---|---|---|
POST | {{body ‘packageType’}} | 从请求体中获取数据 |
GET | {{queryParam ‘batchId’}} | 从请求参数中获取数据 |
GET | {{urlParam ‘type’}} | 从url中获取数据 |
repeat和switch
假设存在接口:
GET /inventory/:type?total=2
type传参有两种:product或materials
返回的数据量等于total的值
type为product时,返回的数据结构为:
[
{
"id":"c9d31bbd-3b55-4c53-8c3d-49a8af4fb6ba",
"name":"Pants",
"price":"485.00 EUR"
}
]
type为materials时,返回的数据结构为:
[
{
"id": "eca2e895-a100-4a86-b1be-ff76fc29068a",
"name": "Granite",
"quantity": "37"
}
]
那么在mockoon中可以定义响应体
[
{{#repeat (queryParam 'total')}}
{
"id": "{{faker 'datatype.uuid'}}",
{{# switch (urlParam 'type')}}
{{# case 'products'}}
"name": "{{faker 'commerce.product'}}",
"price": "{{faker 'commerce.price'}} EUR"
{{/ case}}
{{# case 'materials'}}
"name": "{{faker 'commerce.productMaterial'}}",
"quantity": "{{faker 'datatype.number' 50}}"
{{/ case}}
{{/ switch}}
}
{{/repeat}}
]
生成的数据实例
/content/products?total=2
[
{
"id": "c9d31bbd-3b55-4c53-8c3d-49a8af4fb6ba",
"name": "Pants",
"price": "485.00 EUR"
},
{
"id": "cc147251-6dd5-4dd4-85da-251648eada9e",
"name": "Bike",
"price": "268.00 EUR"
}
]
/content/materials?total=2
[
{
"id": "eca2e895-a100-4a86-b1be-ff76fc29068a",
"name": "Granite",
"quantity": "37"
},
{
"id": "fcaceec9-23d9-4979-9905-c9455569bffc",
"name": "Wooden",
"quantity": "49"
}
]