Skip to content

Instantly share code, notes, and snippets.

@sudipidus
Last active November 27, 2021 12:46
Show Gist options
  • Select an option

  • Save sudipidus/bbc30a90ba0bd2e13984fe05ea6689b4 to your computer and use it in GitHub Desktop.

Select an option

Save sudipidus/bbc30a90ba0bd2e13984fe05ea6689b4 to your computer and use it in GitHub Desktop.
Pre-request script to get current timestamp and echotoken value for API call (for Postman)
//create a current timestamp in javascript
timestamp = new Date();
pm.environment.set(“timestamp”, timestamp);
//function to get a unique identifier to be used as echo token
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + ‘-’ + s4() + ‘-’ + s4() + ‘-’ +
s4() + ‘-’ + s4() + s4() + s4();
}
//setting echotoken value
pm.environment.set(“echotoken”, guid());
//echo tokens and timestamps are used to avoid replay attacks
@sroziewski
Copy link
Copy Markdown

pm is not defined

@sudipidus
Copy link
Copy Markdown
Author

@sroziewski how did you run it? This has to be set as postman pre request script (https://learning.postman.com/docs/writing-scripts/pre-request-scripts/)

@amitblockchaindev
Copy link
Copy Markdown

pm.environment.set is now changed to postman.setEnvironmentVariable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment