使用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
在 Response Body 中可以使用模板来自定义response,比如
{
    "name": "{{$randomFullName}}",
    "userName": "{{$randomUserName}}",
    "location": "{{$randomCity}}",
    "company": "{{$randomCompanyName}}",
    "jobTitle": "{{$randomJobTitle}}",
    "updatedAt": "{{$timestamp}}"
}
如下,这时请求接口返回的结果就会是随机的

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,如下
{
    "name": "{{$randomFullName}}",
    "userName": "{{$randomUserName}}",
    "location": "{{$randomCity}}",
    "company": "{{$queryParams 'abc'}}",
    "jobTitle": "{{$randomJobTitle}}",
    "updatedAt": "{{$timestamp}}"
}
请求接口的时候带上abc=test123,输出就会返回对应的值
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"
}
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/4720752.html
                    
                
                
            
        
浙公网安备 33010602011771号