Postman之Pre-request Script 使用详解

https://blog.csdn.net/testdeveloper/article/details/80712273
https://www.jianshu.com/p/10c49f11d2f7

接口做了防重放,每次需要加nonce进行验证,用postman一个一个加很麻烦,查了下postman有一个很强大的Pre-request-Script功能,可以用脚本,在每次请求前自动加上相应的头
Postman之Pre-request Script 使用详解_第1张图片

脚本如下:

/**
 * 设置重放攻击自动添加请求头
 */
var Header = require('postman-collection').Header;

// before shows 2
printHeaders(pm.request.headers); 

//create timestamp
var timestamp = '' + (new Date()).valueOf();
pm.request.headers.append(Header.create(timestamp,'timestamp'));

//create uuid 
var uuid = require('uuid'); 
var myUUID = '' + uuid.v4();

//create nonce 
pm.request.headers.append(Header.create(myUUID,'nonce'));

//create md5 
var sign = CryptoJS.MD5(timestamp + myUUID).toString();
pm.request.headers.append(Header.create(sign,'sign'));

// after shows 3 with new addition, but doesn't send
printHeaders(pm.request.headers); 

// print out headers to the console
function printHeaders(headers) { 
    console.log('-------pm.request.headers-------')
    headers.each(function(header) {
       console.log(header.key + ': ' + header.value);
    });
}

你可能感兴趣的:(postman)