Sometimes you need to generate new timestamps or id’s or … for every new request you send with POSTMAN. The solution is to use pre-request scripts. These scripts are Javascripts that will use macros to populate values in your body content (json in my case).
With the following JSON
1 2 3 4 5 |
{ "title": "this is a test", "timestamp": "{{TIMESTAMP}}", "id": "{{ID_COUNTER}}" } |
And the following pre-request script
1 2 3 4 5 6 7 8 9 10 11 12 |
var current_timestamp = new Date(); pm.environment.set("TIMESTAMP", current_timestamp.toISOString()); var counter = pm.environment.get("ID_COUNTER") if( counter == null ) { counter = 1; } else { counter = parseInt( counter ) + 1; } counter = String(counter).padStart(4,'0'); console.log("counter "+(counter)); pm.environment.set("ID_COUNTER",counter); |
then you can get the following result
1 2 3 4 5 |
{ "id": "0003", "timestamp": "2023-05-30T13:00:27.372Z", "title": "this is a test" } |