Postman is a powerful tool, but it’s hard not to write a simple blog post about the use of pre-Request scripts, which is an advanced version of the basic use.
The background,
When we were testing the API, many times the request parameters involved some encrypted parameters, such as signatures.
When we used Postman for testing, we wanted Postman to automatically do the calculations for us and pass them as parameters once we had defined the encryption algorithm for the signature.
This paper first around the automatic signature generation example, involving Base64 encoding and MD5 encryption.
Second, the use of
1. Placeholder description
For a first request address of chestnut: http://localhost:9090/apps/task/inWorkList/ {{token}}? signed={{signed}}
Notice that the tokens and signed placeholders in the address are wrapped in two pairs of curly braces, and then we do the calculation elsewhere and replace the argument here,
Replace after the address is: http://localhost:9090/apps/task/inWorkList/1qaz2wsx? signed=1qaz2wsx
Note: The same placeholder notation can be applied to URI, Headers, and Body.
2. Introduction of variables
The placeholders described above are actually derived from Postman variables.
In Postman, variables are distinguished by global variables and environment variables, and are k-V key-value pairs.
Json.stringify () and json.parse () are used to serialize and deserialize values.
As the name suggests, global variables can be used anywhere, whereas environment variables are used only in the current environment. Of course, if the same key is defined in both places, such as the k-V key-value pairs of signed: 1qAZ2WSX, environment variables have higher precedence.
3. Variable management
There are two ways. One is to operate directly on the client, as shown in the figure:
In the pre-request area, use the code to get and set.
// Clear global variables
pm.globals.unset("variable_key");
// Clear environment variables
pm.environment.unset("variable_key");
// Get global variables
pm.globals.get("variable_key");
// Get variables
pm.variables.get("variable_key");
// Get environment variables
pm.environment.get("variable_key");
// Set global variables
pm.globals.set("variable_key"."variable_value");
// Set environment variables
pm.environment.set("variable_key"."variable_value");
Copy the code
In fact, there are common operations on the client tips:
4. Code details
Tokens and signed tokens are generated using a timestamp (20181225224000) that is slowly concatenated using JS Date().
The token is base64 encoding.
Signed is MD5 encryption.
/ / variable
// uid
var uid = '7777'
var api_sig_str = pm.variables.get("api_signed_str");
/ / time
var d = new Date(a)var year = d.getFullYear().toString()
var month = (d.getMonth() + 1).toString()
month = dateLengtgInit(month)
var day = d.getDate().toString()
day = dateLengtgInit(day)
var hour = d.getHours().toString()
hour = dateLengtgInit(hour)
var minute = d.getMinutes().toString()
minute = dateLengtgInit(minute)
var second = d.getSeconds().toString()
second = dateLengtgInit(second)
var time = year + month + day + hour + minute + second
console.log(time)
// token (base64)
var token = CryptoJS.enc.Base64.stringify(uid + time)
console.log(token)
// postman.setGlobalVariable("token",token)
pm.environment.set("token",token)
//signed (md5 encryption)
var signed = api_sig_str + '_' + uid + '_' + time
console.log(signed)
var signedMd5 = CryptoJS.MD5(signed).toString()
console.log(signedMd5)
// pm.globals.set("signed",signedMd5)
pm.environment.set("signed",signedMd5)
// Method definition
function dateLengtgInit(num){
if (num.length==1) {return "0"+num;
} else {
return num
}
}
Copy the code
5, hit the pit
An error is reported when executing:
There was an error in evaluating the Pre-request Script: TypeError: r.clamp is not a function
After debugging, we found that the base64 encoding was wrong. We need to encode Utf8 first.
The next piece of code: var token = CryptoJS enc. Base64. Stringify (uid + time)
Change into: var token = CryptoJS. Enc. Base64. Stringify (CryptoJS. Enc. Utf8. Parse (uid + time))
Three, reference
Official documentation: Link address
Original address: www.jetchen.cn/postman-pre…