If I will send this request again at the same time, it went to duplicate
Now, I have 50 requests with different data set, but if I send this before 3 hours then it went to duplicate, I am running this using, collection runner, so is there any way that my request will take every time 3+ hours to startTime for every iteration?
So here why do you need specifically to add three hours? You can use the current timestamp for every run, by defining the format in the Pre-request script section.
let now = new Date().toISOString();
console.log('Now the time is:' + now);
pm.globals.set("now",now)
if you are okay with using the current time stamp, paste the above under your pre-request script section. and use the variable {{now}} against the startTime field in the body.
Else if you still need plus three hours, please use the below script under pre-request script section and use the variable {{timebegin}} in your body tab.
var time = new Date();
time.setHours(time.getHours()+3);
var timebegin = time.toISOString();
console.log('After three hours time will be:' + timebegin);
pm.globals.set("timebegin", timebegin)
I tried the above solution but, not getting success (my body have backend rule second request we can not send before 3+ hours)
Like → I have this one only request In my folder now try to run this from collection runner same request again then it went to the duplicate on greater than 1 iteration (just because of time)
after the above solution, iteration take this below StartTime for 3 iterations
After three hours will be:2021-01-12T10:08:11.402Z
After three hours will be:2021-01-12T10:08:13.432Z
After three hours will be:2021-01-12T10:08:14.273Z
Only impacted in seconds but we need + 3hours difference in each iteration from collection runner.
Okay @AbhishekTiwari , if I get correctly you need to add three hours from the previous sent time correct? Can you please try the below snippet?
var time = new Date();
function three()
{
console.log('Now the time is:' + time.toISOString());
time.setHours(time.getHours()+3);
result = time.toISOString();
console.log('After three hours time will be:' + result);
return result;
}
//console.log(pm.info.iterationCount);
for(i=0;i<pm.info.iterationCount;i++)
{
three();
pm.globals.set("timebegin", result)
}
Getting error and time is not logging in the global variable, using above snippet
{“errorCode”:“POL1015”,“errorCodeMessage”:“Invalid Parameter”,“errorSubCode”:“Invalid JSON Request parameters”,“errorSubCodeMessage”:“startTime: /{{result}}/ doesn’t match pattern \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z”,“errorSubCodeDescription”:“startTime: expected format yyyy-MM-ddTHH:mm:ss.SSSZ”,“transactionTime”:“2021-01-12T10:46:26 +0000”}
Please check. I have tried this, but the postman getting hanged using the below script.
var dt = new Date();
for(dt>=0;dt=01;dt++){ (confused regarding which loop we need to initiate for iteration or for time)
if(itration = 1 )
{
pm.environment.set(‘dt’,dt);
}
else dt.setHours( dt.getHours() + 3 );
{
pm.environment.set(‘dt’,dt);
}
};
I tried with timebegin this one is also taking the same,
first iteration - “startTime”: “2021-01-12T22:03:05.650Z”
second iteration - “startTime”: “2021-01-12T22:03:06.650Z”
third iteration - “startTime”: “2021-01-12T22:03:07.650Z”
I need these three iterations with the below startTime
first iteration - “startTime”: “2021-01-12T03:03:05.650Z”
second iteration - “startTime”: “2021-01-12T06:03:06.650Z”
third iteration - “startTime”: “2021-01-12T09:03:07.650Z”
second iteration startTime should be + 3hours in the first iteration start time. third iteration startTime should be + 3hours in the second iteration start time.
Thanks for sharing this, but in this case I need to send approx 20+ requests with the same kind of data, the only difference is startTime should plus 3 hours than the first request this rule is applicable for all 20+ requests time gap between each request should be, 3+ hours. must be run using collection runner
Then I’d say what @bpricilla is saying is the right way to go down. The only suggestion I’d make is to use moment instead of the javascript date just to make it a little easier to do the date math.
Pre-request script
var moment = require('moment');
let iterationCount = pm.collectionVariables.has('iterationCount') ? pm.collectionVariables.get('iterationCount') : 0;
pm.collectionVariables.set('startTime', moment().add(3 * iterationCount, 'hours').format());
pm.collectionVariables.set('iterationCount', (iterationCount + 1));
Test script
let iterationCOunt = pm.collectionVariables.get('iterationCount');
if(iterationCOunt < 20) {
postman.setNextRequest(request.name);
}