欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PostMan使用案例(1)

程序员文章站 2022-04-10 17:10:33
...

postman客户端下载地址:https://www.getpostman.com/apps

1.接口请求的使用
第一步:将接口放URL中,选取请求方式,
PostMan使用案例(1)
第二步:填入请求头信息,参数,如果是post请求,则将参数内容放在body中。
当请求参数值从环境变量中取时,用{{}}括起来,PS:当有环境变量时,记得在右上角选择你定义的那个环境变量,不然会读取环境变量失败
PostMan使用案例(1)
PostMan使用案例(1)
PostMan使用案例(1)
第三步:修改环境变量
点击右上角的眼睛图标,可以查看当前环境变量的值,点击Edit,可以对环境变量进行编辑
PostMan使用案例(1)
PostMan使用案例(1)
第四步:验证或获取返回数据
我验证的接口的返回值:

{"code":0,"message":null,"data":{"id":123456,"loginName":"654321","nickname":null,"realName":"王丹杨","gender":null,"birthday":null,"phone":"15150477114","email":null,"countryId":null,"provinceId":836,"cityId":876,"countyId":null,"address":null,"idCard":null,"avatar":null,"sourceFrom":null,"status":1,"openIdQQ":null,"deviceToken":"AnAm3NhdSgw-uxSOlCd6olHT-Fu21l7RGCNBcOnFNHDj","appPush":null,"schoolStudentInfoBO":{"id":123456,"schoolId":2772,"schoolName":"【测试】测试学校(初中)","jkCode":"654321","studentName":"王丹杨","gender":4,"birthday":null,"idCard":null,"studentNo":"909090","studentCode":"1710106","studentType":2,"phone":"15150477114","email":null,"address":null,"bound":true,"studentId":123456,"clzssesInfo":[{"id":59685,"schoolId":2772,"gradeId":4646,"baseGradeValue":2,"schoolYear":2018,"clzssName":"1班","clzssType":1,"fullName":"1班","sort":2,"finished":null,"status":null,"gradeName":"初二","studentClassSubjects":[{"id":111640,"schoolId":2772,"baseSubjectValue":1,"gradeId":4646,"schoolSubjectName":"数学","sort":1},{"id":111641,"schoolId":2772,"baseSubjectValue":2,"gradeId":4646,"schoolSubjectName":"语文","sort":2},{"id":111642,"schoolId":2772,"baseSubjectValue":3,"gradeId":4646,"schoolSubjectName":"英语","sort":3},{"id":111643,"schoolId":2772,"baseSubjectValue":4,"gradeId":4646,"schoolSubjectName":"物理","sort":4},{"id":111644,"schoolId":2772,"baseSubjectValue":5,"gradeId":4646,"schoolSubjectName":"化学","sort":5},{"id":111645,"schoolId":2772,"baseSubjectValue":6,"gradeId":4646,"schoolSubjectName":"生物","sort":6},{"id":111646,"schoolId":2772,"baseSubjectValue":7,"gradeId":4646,"schoolSubjectName":"政治","sort":7},{"id":111647,"schoolId":2772,"baseSubjectValue":8,"gradeId":4646,"schoolSubjectName":"历史","sort":8},{"id":111648,"schoolId":2772,"baseSubjectValue":9,"gradeId":4646,"schoolSubjectName":"地理","sort":9},{"id":111649,"schoolId":2772,"baseSubjectValue":10,"gradeId":4646,"schoolSubjectName":"信息","sort":10}]}]}},"total":null}

需要更好看的查看请使用json转换
我要获得clzssesInfo里面的数据并保存到全局环境变量中,在下个接口使用这些数据。

var jsonData = JSON.parse(responseBody);
//学生id
postman.setGlobalVariable("studentid", jsonData.data.id);
//get city id
postman.setGlobalVariable("cityId", jsonData.data.cityId);
//get schoolid
postman.setGlobalVariable("schoolId", jsonData.data.schoolStudentInfoBO.schoolId);
//get jikenum
postman.setGlobalVariable("jkCode", jsonData.data.schoolStudentInfoBO.jkCode);

var classinfo = jsonData.data.schoolStudentInfoBO.clzssesInfo;
//classinfo返回是个OBJECT类型的数组,拿下标为0的值
var obj = classinfo[0];
postman.setGlobalVariable("baseGradeValue",obj.baseGradeValue);
//get classid
postman.setGlobalVariable("clzssid",obj.id);
//get gradeid
postman.setGlobalVariable("gradeId",obj.gradeId);

验证接口返回是否是200,

//判断执行结果
var message = jsonData.message;
if (responseCode.code === 200) ;
tests["接口执行成功,code返回200"] = responseCode.code ===200;

if (message === "null");
tests["接口执行成功,message为null"] = responseCode.code ===200;
if(message != "null");
tests["接口执行失败,错误信息"+message];

第五步:Collection使用
PostMan使用案例(1)
添加Collection,将需要一起run的API添加到一个collection中,并取见名知意的名字。
PostMan使用案例(1)

PS:

每个接口之间跑的顺序可以在Tests定义,最后一个接口一定要定义终止程序,否则会进入死循环。

// the next api
postman.setNextRequest("收藏题目");
//是一个终止程序作用
postman.setNextRequest("null");

使用技巧:
打开console控制台快捷键:Ctrl+Alt+C,方便debug脚本

相关标签: postman