Send request after x amount of time passed, otherwise skip

Hi, all.
Is there a way, using Postman and ideally run the that collection in newman later, to repeat a collection that has three requests multiple times but send the first request every, say, 20 minutes?

We plan to use newman as a helper tool to perform load/stress tests on our system. Since newman doesn’t handle Oauth 2.0 authentication set on Authentication tab in the Postman collection, I need to make a request to fetch bearer token from Keycloak.

During our stress tests, I don’t want to simply loop over the collection because that would send a request to our KC server with every loop. Is there a way to write a contidtion in pre-request or test tab that will fire the KC request on the first loop and then every X minutes or X loops, otherwise it should skip the KC request and just send the requests that we use to test our system…

Store the expiry date of the token, and instead of using the authentication helper, you can have a pre-request script that mimics the functionality.

The following is an example of authenticating to Microsoft.

let currentDateTime =;
let tokenExpiry = pm.environment.get("bearerTokenExpiresOn")
// console.log("currentDateTime: " + currentDateTime);
// console.log("tokenExpiry: " + tokenExpiry);
if (!pm.environment.get("bearerToken") || currentDateTime > tokenExpiry) {
    pm.test("Pre-request check for Environment Variables", function () {
        let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
        vars.forEach(function (item) {
            // console.log(item);
            pm.expect(pm.environment.get(item), item + " variable not set");
            pm.expect(pm.environment.get(item), item + " variable not set");
            url: '' + pm.environment.get("tenantId") + '/oauth2/v2.0/token',
            method: 'POST',
            header: 'Content-Type: application/x-www-form-urlencoded',
            body: {
                mode: 'urlencoded',
                urlencoded: [
                    { key: "client_id", value: pm.environment.get("clientId"), disabled: false },
                    { key: "scope", value: pm.environment.get("scope"), disabled: false },
                    { key: "username", value: pm.environment.get("username"), disabled: false },
                    { key: "password", value: pm.environment.get("password"), disabled: false },
                    { key: "client_secret", value: pm.environment.get("clientSecret"), disabled: false },
                    { key: "grant_type", value: "password", disabled: false },
        }, function (err, res) {
            if (err) {
            } else {
                pm.test("Pre-request Microsoft login Status code is 200", () => {
                    let resJson = res.json();
                    // console.log(resJson);
                    pm.environment.set("bearerToken", resJson.id_token);
                    pm.environment.set("bearerTokenExpiresOn", + resJson.expires_in * 1000);
                    // console.log("bearerTokenExpiresOn: " + pm.environment.get("bearerTokenExpiresOn"));

However, please note, this won’t persist through collection runs. You will have to authenticate at least once per collection run.

Thanks, man. This works!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.