使用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,比如

{
    "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"
}

 

posted @ 2015-08-11 13:24  tonglin0325  阅读(211)  评论(0编辑  收藏  举报