How can I send, 3+ hours startTime to the request for every iteration

For example, my: request is
{
“name”: “the New-test”,
“charge”: {
“type”: “Ask”,
“code”: 123,
“category”: “newProduct”,
“service”: {
“name”: “qaz”,
“type”: “brandQA”,
“amount”: 0.10,
“currency”: “CAD”,
“unitsConsumed”: 1,
“content”: {
“size”: “6”,
“unitOfMeasure”: “pic”
}, } },
“thirdPartyAccount”: {
“id”: “test”,
“name”: “test01”,
“partnerId”: “namessid”,
“ssid”: “new001”,
“device”: “mobile”
},
"startTime": "11-01-2021T03:17:08.000Z"
}

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?

Hello @AbhishekTiwari, Warm welcome to the community :bouquet:

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)

Please let me know if you have any issues :relaxed:

Hi @bpricilla
thanks for responding…!

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

  1. After three hours will be:2021-01-12T10:08:11.402Z
  2. After three hours will be:2021-01-12T10:08:13.432Z
  3. 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.

image

trying to send the same request 3 times

image

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)
}

@bpricilla

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”}

image

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);
}
};

You can set up a monitor to run every three hours, would that solve your use case?

@AbhishekTiwari You need to use the variable {{timebegin}} not result.

@bpricilla

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.

@allenheltondev

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);
}
2 Likes

if your start time is in the format YYYY-MM-DDTHH:MM:ss.SSSZ , then use:

const moment = require('moment');

if (pm.info.iteration === 0) {

    pm.collectionVariables.set("startTime", moment.utc())

} else {

    let measuredOnUtc = pm.collectionVariables.get("startTime")

    measuredOnUtc = moment(measuredOnUtc)

    pm.collectionVariables.set("startTime", measuredOnUtc.add(3, 'hours').utc())

}

if startime is in format DD-MM-YYYYTHH:MM:ss.SSS[Z] , then use:

const moment = require('moment');

if (pm.info.iteration === 0) {

    pm.collectionVariables.set("startTime", moment.utc().format("DD-MM-YYYYTHH:MM:ss.SSS[Z]"))

} else {

    let measuredOnUtc = pm.collectionVariables.get("startTime")

    measuredOnUtc = moment(measuredOnUtc, "DD-MM-YYYYTHH:MM:ss.SSS[Z]")

    pm.collectionVariables.set("startTime", measuredOnUtc.add(3, 'hours').format("DD-MM-YYYYTHH:MM:ss.SSS[Z]"))

}

THe request body should be :slight_smile:

{

bbla:bla,

"startTime": "{{startTime}}",

bbla:bla
}
1 Like

Thanks to all for giving solutions.

for me below code is working thanks… @praveendvd

const moment = require(‘moment’);
if (pm.info.iteration === 0) {
pm.collectionVariables.set(“startTime”, moment.utc())
} else {
let measuredOnUtc = pm.collectionVariables.get(“startTime”)
measuredOnUtc = moment(measuredOnUtc)
pm.collectionVariables.set(“startTime”, measuredOnUtc.add(3, ‘hours’).utc())
}

2 Likes