postman发送request请求

postman发送request请求

//更新数据点列表
//pm.variables.get("baseUrl") --> 设置的bseUrl变量:host:port
//pm.environment.get("path") --> 需要发送的请求路径
//pm.environment.get("headers") -->需要发送的请求的请求头信息
var reqUrl= "http://"+pm.variables.get("baseUrl")+pm.environment.get("path");
const reqRequest= {
  url: reqUrl,
  method: 'POST',
  header: pm.environment.get("headers"),  //注意要在Header中声明内容使用的类型
  body: {
    mode: 'raw',  // 使用raw(原始)格式
    raw: JSON.stringify({deleted: false }) //要将JSON对象转为文本发送
  }
};
//发送请求
pm.sendRequest(reqRequest,function (err, res) {
    console.log(err ? err : res.json());  // 响应为JSON格式可以使用res.json()获取到JSON对象
    //pm.environment.get("orgJSONData ") --> 原始JSON数组
    var orgJSONData = pm.environment.get("orgJSONData ");
        pm.test("新增结果检查--"+"列表数据"+res.json().data.length+"条", function () {
            pm.expect(res.json().data.length).to.eql(orgJSONData .length+1);
        });
    //更新orgJSONData:如果请求发送错误,orgJSONData值不变,如果正常返回,更新orgJSONData
    orgJSONData = err ?  orgJSONData : res.json().data;
    //更新项目列表
    SetEnvironment("orgJSONData ", orgJSONData );
    //SetEnvironment("orgIDs",GetList(orgJSONData ,"id")); 
    //SetEnvironment("orgNames",GetList(orgJSONData ,"name"));  
    //SetEnvironment("orgCodes",GetList(orgJSONData ,"code"));
    //获取新增数据值
    var myCode = pm.environment.get("myCode");     
    var myID = GetValue(orgData,myCode,"code","id");               
    SetEnvironment("mytID",myID);
});

//获取列表
function GetList(orgData, key) {
    var dataList = [];
    for (var i = 0; i < orgData.length; i++) {
        dataList[i] = orgData[i][key];    //数据列表添加值
    }
    return dataList;
}
//获取值
//myDataList --> JSON数组
//myValue --> 唯一值
//keyIn --> myValue对应的key
//keyOut --> 需要获取的值对应的key
function GetValue(myDataList,myValue,keyIn,keyOut){
    var mydata = "";
    for(var i=0; i<myDataList.length; i++){
        if(myValue == myDataList[i][keyIn]){
            var mydata = myDataList[i][keyOut];
        }
    }
    return mydata;
}
//设置环境变量
 function SetEnvironment(myKey,myValue){
    pm.environment.set(myKey,myValue);
}

你可能感兴趣的:(接口测试,postman,json,测试工具)