tonglin0325的个人主页

使用postman创建mock server

可以使用postman创建一个mock server用于临时测试API,参考官方文档:Configure and use a Postman mock server

选择Mock servers,点击+号创建一个mock server

创建

最后会得到一个URL,这就是mock server请求的URL

测试一下

查看postman的logs

使用mock server也可以使用变量和模板定义response

参考:https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/creating-dynamic-responses/

在 Response Body 中可以使用模板来自定义response,比如

1
2
3
4
5
6
7
8
9
{
"name": "{{$randomFullName}}",
"userName": "{{$randomUserName}}",
"location": "{{$randomCity}}",
"company": "{{$randomCompanyName}}",
"jobTitle": "{{$randomJobTitle}}",
"updatedAt": "{{$timestamp}}"
}

 如下,这时请求接口返回的结果就会是随机的

1
2
3
4
5
6
7
8
9
10
curl https://xxxx.mock.pstmn.io/tonglin0325
{
"name": "Sara Kassulke",
"userName": "Kari.Leannon",
"location": "Adriannabury",
"company": "Gleichner, Lowe and Christiansen",
"jobTitle": "Customer Branding Manager",
"updatedAt": "1724255442"
}

也可以指定输出的字段为请求的参数abc,如下

1
2
3
4
5
6
7
8
9
{
"name": "{{$randomFullName}}",
"userName": "{{$randomUserName}}",
"location": "{{$randomCity}}",
"company": "{{$queryParams 'abc'}}",
"jobTitle": "{{$randomJobTitle}}",
"updatedAt": "{{$timestamp}}"
}

请求接口的时候带上abc=test123,输出就会返回对应的值

1
2
3
4
5
6
7
8
9
10
curl https://xxxx.mock.pstmn.io/tonglin0325\?abc\=test123
{
"name": "Lyle Baumbach",
"userName": "Van_Jacobson",
"location": "South Shayna",
"company": "test123",
"jobTitle": "Internal Identity Strategist",
"updatedAt": "1724470931"
}