Postman/apifox pre-request script
Postman/apifox pre-request script
const UUID = require('uuid'); // Helper function to generate the signature function makeSign(md5Key, params) { const preStr = buildSignString(params); const text = preStr + md5Key; console.log("signString=",text); return CryptoJS.MD5(text).toString().toUpperCase(); } // Helper function to build the sign string function buildSignString(params) { if (!params || Object.keys(params).length === 0) { return ""; } const keys = Object.keys(params).filter(key => key !== "sign" && params[key] !== null && params[key] !== ""); keys.sort(); let signString = ""; for (let i = 0; i < keys.length; i++) { const key = keys[i]; const value = typeof(params[key])==='object'? JSON.stringify(params[key]):params[key].toString(); if (i === keys.length - 1) { signString += `${key}=${value}`; } else { signString += `${key}=${value}&`; } } return signString; } // Parse the JSON object // const jsonString = `{"creationId":"190017","status":"1","remark":""}`; const requestBody = pm.request.body; const jsonString = requestBody.raw; const params = JSON.parse(jsonString); // Generate the nonce and timestamp const nonce = UUID.v4();; const timestamp = Math.floor(Date.now() / 1000).toString(); // Set the application key const appKey = "rc8nlpfaqiri8l9g"; params.nonce = nonce; params.applicationKey = appKey; params.timestamp = timestamp; console.log(params) // Generate the sign const appSecret = "ncbj91gr5y5w5ux11a0e7kfash"; const sign = makeSign(appSecret, params); // Set the headers const headers = { "appKey": appKey, "sign": sign, "timestamp": timestamp, "nonce": nonce, }; pm.environment.set("headers", headers); pm.environment.set("appKey", appKey); pm.environment.set("sign", sign); pm.environment.set("timestamp", timestamp); pm.environment.set("nonce", nonce);
请求时增加参数占位符
curl --location --request POST 'http://127.0.0.1/v1/getMember' \ --header 'Application-Key: {{appKey}}' \ --header 'timestamp: {{timestamp}}' \ --header 'nonce: {{nonce}}' \ --header 'signature: {{sign}}' \ --header 'Content-Type: application/json' \ --data-raw '{ "mobile": "15815593592" }'