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

postman-编写脚本

程序员文章站 2022-03-05 18:21:55
...

Scripts in Postman - 官方

使用场景:
当我们使用postman请求网络资源时(如restful api),通常需要生成签名才能调用资源,这时编写脚本就变得非常重要了。

下面代码实现postman调用前生成签名(示例),在postman的Pre-request Scripts写相关代码即可:

var url = pm.request.url.toString();
url = pm.environment.replaceIn(url);
console.log('yibi.url: '+url);

var urlHost = pm.environment.replaceIn(pm.request.url.getHost());
console.log('yibi.urlHost: '+urlHost);

var urlLeft = url.lastIndexOf('?') > -1 ? url.substring(0,url.lastIndexOf('?')):url;
urlLeft = pm.environment.replaceIn(urlLeft);
console.log('yibi.urlLeft: '+urlLeft);

var urlPath = pm.request.url.getPath();
console.log('yibi.urlPath: '+urlPath);

var httpMethod = request.method;
console.log('yibi.httpMethod: '+httpMethod);

var queryParam = pm.request.url.getQueryString({ignoreDisabled:true}); //get 参数
console.log('yibi.queryParam: '+queryParam);

var formData = pm.request.data;  //post参数
console.log('yibi.formData: '+formData);

var timestamp = getTimestamp();
console.log('yibi.timestamp: '+timestamp);

var dictParms = appendParams(queryParam,formData);
parmsSort(dictParms);

var secretKey = 'yourvalue';
var signatureString = signature(secretKey,urlPath,dictParms);
console.log('signatureString: '+signatureString);

function getTimestamp(){
    return pm.variables.replaceIn('{{$timestamp}}');
}

function appendParams(queryString,formData){
    console.log('appendParams--queryString: '+queryString+',formData: '+formData+'');
    var urlParms = getUrlParms(queryString);
    if(formData){
        var keys = Object.keys(formData);
        for (var i=0; i<keys.length; i++) {
            var key = keys[i];
            var value = formData[key];
            console.log('getParams--Key: '+key+',value: '+value+'');

            urlParms[key] = value;
        } 
    }

    return urlParms;
}

function parmsSort(dict)
{
    var keys = Object.keys(dict);
    keys.sort(); 
}

function getUrlParms(queryString){
    var keyValues = {};
    var elements = queryString.split ("&");
    for(var i in elements) { 
        var key = elements[i].split("=");
        if (key.length > 1) {
            keyValues[decodeURIComponent(key[0].replace(/\+/g, " "))] = decodeURIComponent(key[1].replace(/\+/g, " "));
        }
    }

    return keyValues;
}

function toQueryString(dict){
    var keys = Object.keys(dict);
    keys.sort();

    return keys.map(key => key + '=' + dict[key]).join('&');

    //或使用下面方法:
    // var res = '';
    // let keys = Object.keys(dict).sort();
    // for (let i in keys) {
    //     var key = keys[i];
    //     var value = dict[key];
    //     if(i > 0){
    //         res += '&';
    //     }
    //     res += ''+key+'='+value+'';
    // }

    // return res;
}

//签名
function signature(secretKey,url,dictParms){
    var inputString = '';
    var queryString = toQueryString(dictParms);
    if(queryString.trim() != ''){
        inputString = (url + '?' +toQueryString(dictParms)).trim('?');
    }
    
    return signatureByMd5(secretKey,inputString);
}

//使用md5签名字符串
function signatureByMd5(secretKey,inputString){
    console.log('yibi.signatureByMd5--secretKey: '+ secretKey + ',inputString: '+inputString);
    var md5=CryptoJS.MD5(inputString+secretKey);
    var md5Base64 = CryptoJS.enc.Base64.stringify(md5);
    console.log('yibi.sign--md5Base64: '+ md5Base64);
    pm.environment.set("Signature", md5Base64);

    return md5Base64;
}
相关标签: postman