This is the second day of my participation in the More Text Challenge. For details, see:More article challenges
Postman is a tool that can help you and even your team develop apis quickly.
Postman recently discovered a great feature called pre-Request Script, which allows you to write JavaScript scripts to automate tasks before a request is made.
This article focuses on how to automatically refresh interface credentials using a pre-request script.
aboutPostman
Postman is a collaborative platform for interfaces. Postman’s various features make it easy to build an interface and improve the efficiency of team collaboration to create better interfaces faster!
Prerequest script
In the pre-request script, the PM object provides most of the important functions, including pm.environment, pm.sendrequest, etc., which can be used to test our request content and response results.
In Postman, you can create a collection of environments. Using the Environment, you can set variables such as baseUrl and use them in the Request address bar like this:
{{baseUrl}}/user/get
Copy the code
At this point, in the pre-request script, we can get and set the contents of the Environment in the following ways:
// Obtain the token environment variable
const token = pm.environment.has('token')
// Set the token environment variable
pm.environment.set('token'.'xxx')
Copy the code
Pre-requested scripts have many other capabilities, as you can see in this document.
The interface credentials are automatically refreshed
Now there is a scenario: an interface service requires that the request should carry a credential token, and the token is only valid for two hours. If there is no pre-request script, we need to obtain the token every two hours. This action is manual, so it is very tedious.
But with the pre-request script, we can set the pre-request script in the interface collection, so that all requests in the interface collection will be executed before the pre-request script. The pre-request script will automatically obtain the interface token and set it in the environment variable. In this case, The interface request is correct!
Here is the pre-request script I wrote:
const d = new Date(a);const t = d.getTime();
const lastRefreshTokenTime = parseInt(pm.environment.get("lastRefreshTokenTime"));
if (t-lastRefreshTokenTime < 3600000) {
return
}
const url = pm.environment.replaceIn("{{baseURL}}/api/refreshToken")
const req = {
url: url,
method: 'POST'.header: {
'Content-Type': 'application/json'
},
body: {
mode: 'raw'.raw: JSON.stringify({
appId: pm.environment.get('AppID'),
appSecret: pm.environment.get('AppSecret')})}}; pm.sendRequest(req,function (err, response) {
if (response.code === 200) {
const data = response.json()
pm.environment.set('token', data.token)
pm.environment.set('lastRefreshTokenTime', t)
return
}
console.log(`Error: ${response}`)});Copy the code
In addition to automatically refreshing the token, the preceding pre-request script also uses the environment variable lastRefreshTokenTime to determine the last token acquisition time. If the interval is more than one hour, The token is refreshed and lastRefreshTokenTime is set to the current timestamp. Otherwise, the token is still valid and will not be refreshed.
Personal blog
K8scat.com/posts/auto-…