在postman中加入md5功能,像登陆验证一般是经过加密的,所以在Postman上也要设置一下此功能,并且还可以设置获取当前时间戳功能,不用自己每次手动去获取一个新的时间戳。
1、先在postman的Pre-request Script中插入一段代码(下面会把完整代码帖出来,复制进入即可),这个功能是可以自定义开发你自己需要的功能,这只演示md5的功能。
图文中我只标注了把指定字符串加密码成md5,在加密下一行代码中就是对外暴露变量的,暴露了一个变量叫md5,后面是读取的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//发起请求之前获取当前的时间戳放在参数里 //postman.setGlobalVariable("customerCode","***2345677***"); //1.设置环境变量 postman.setEnvironmentVariable("key", "value"); //2.设置全局变量 postman.setGlobalVariable("key", "value"); //environment.customerCode = "***2345677***"; customerCode = postman.getGlobalVariable("customerCode"); //设置当前时间戳毫秒 postman.setGlobalVariable("timestamp",Math.round(new Date().getTime())); //environment.timestamp = Math.round(new Date().getTime()); //postman.setEnvironmentVariable("unixtime_now","timecode"); //var jsonData = JSON.parse(request.data.applyJsonStr); //postman.setGlobalVariable("ytoken","*********b176a4739bfccb*********"); //获取全局变量 //如postman.getGlobalVariable("key"); customerCode = postman.getGlobalVariable("customerCode"); timestamp = postman.getGlobalVariable('timestamp'); ytoken = postman.getGlobalVariable("ytoken"); var str = customerCode+timestamp+ytoken; //postman.setEnvironmentVariable("str",str); //environment.str = str; postman.setGlobalVariable("str",str); //var md5 = CryptoJS.MD5(str).toString().toLowerCase(); //使用md5加密 //var strmd5 = CryptoJS.MD5(str).toString(); var strmd5 = CryptoJS.MD5('123456'); //environment.strmd5 = strmd5; postman.setGlobalVariable('md5',strmd5); //environment.md5 = md5; //timecode=System.currentTimeMillis(); console.log(str); |
2、下面去请求发送参数栏使用 指定变量即可,发送请求的时候就是经过加密的md5字符串了