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

Postman 使用脚本及变量

程序员文章站 2022-07-12 11:47:46
...

Tests

var jsonData = JSON.parse(responseBody);
if (jsonData.status == 200){
    postman.setEnvironmentVariable("token", jsonData.data.token);
}

变量

// 没什么好说的

Pre-Script 自动签名

let form_params = {};
Object.assign( form_params, getGet(), getPost());
form_params.timestamp = (new Date()).getTime().toString();

query_string = http_build_query( ksort( form_params))+pm.environment.get("__SALT__");
console.log( query_string);
form_params.sign = CryptoJS.MD5( query_string).toString();

pm.environment.set('__TIMESTAMP__'  , form_params.timestamp);
pm.environment.set("__SIGN__"       , form_params.sign);

function ksort(obj){
    let keys = Object.keys(obj).sort();
    let arr = {};
    for (let i in keys) if( keys[i] != 'sign') arr[keys[i]] = obj[keys[i]]
    return arr;
}
function http_build_query( obj){
    query_string = '';
    for( let i in obj) query_string += i+"="+obj[i]+"&";
    return query_string.slice(0,-1);
}
function getPost(){
    return request.data;
}
function getGet(){
    res = {}; members = pm.request.url.query.members; 
    for( let i in members) res[members[i]['key']] = members[i]['value'];
    return res;
}