Last active
November 27, 2021 12:46
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |
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/)
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
pm is not defined