Call request from collection by its name from 'Pre-request script'

Hi!
I have specific auth request in my collection. i want to use it as pre-step.
Is it possible to call request from the collection by name from ‘Pre-request script’?
I found only pm.sendRequest as option, but i don’t want to specifiy header/body etc. just to call one existed already request from my collection by its name.

1 Like

Hi @eugenes2020 !

This page may be useful for your situation Building request workflows | Postman Learning Center
:slight_smile:

You’re on the right track with wanting to streamline your workflow in Postman. To address your specific requirement:

  1. Sequence Your Requests: Organize your collection so your authentication request is placed directly before the actual request you wish to run.
  2. Using pm.setNextRequest: In the Tests script of your authentication request, simply add pm.setNextRequest("Name of Your Target Request"). This means after your authentication request runs, Postman will immediately execute the named request you specify.
  3. Initiate with Runner: Use the Postman Runner to start your collection, specifically initiating the authentication request. With the guidance from pm.setNextRequest, the following request in your sequence will execute automatically.
  4. Data Transfer: If your authentication request generates tokens or other data for the next request, use pm.environment.set() to save these. They’ll be available for the subsequent requests in the sequence.

Essentially, instead of a “pre-step”, you’re setting up a specific order of execution using the Runner. Your auth request acts as a precursor to the main request, making the process seamless.

Hope this clears things up! Let me know if you have further queries.

Hi, @kevinc-postman!
Thanks for your response!
In my case I’m not looking to build a workflow. My idea is just manually ‘Send’ single request for specific validation. That’s why I thought to use something like ‘sentNextRequest’ but make it ‘previous’, instead of run each time auth-request before. It’s not big deal, just curious if it possible.

1 Like

You can’t really re-use a request as it won’t know what request to run next without using setNextRequest which means it can only be controlled by one request which defeats the purpose.

This should probably be a feature request on the Postman Github, as most automated testing tools have the concept of re-use of test cases\artefacts. You should be able to call a particular request and after executing it should return to the existing request. You should be able to call a request from a pre-request script or the tests tab.

You are probably better just using sendRequest() for this as you originally indicated. Which you can stick in a pre-request script at the collection level.

You can wrap the request in an IF statement that only gets a new token if the current one has expired (or hits a particular threshold).

The following is an example of authenticating to Microsoft.

pm.test("Check for collectionVariables", function () {
    let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
    vars.forEach(function (item, index, array) {
        console.log(item, index);
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined;
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty; 
    });

    if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) {
        pm.sendRequest({
            url: 'https://login.microsoftonline.com/' + pm.collectionVariables.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.collectionVariables.get("clientId"), disabled: false },
                    { key: "scope", value: pm.collectionVariables.get("scope"), disabled: false },
                    { key: "username", value: pm.collectionVariables.get("username"), disabled: false },
                    { key: "password", value: pm.collectionVariables.get("password"), disabled: false },                    
                    { key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false },
                    { key: "grant_type", value: "password", disabled: false },
                ]
            }
        }, function (err, res) {
            if (err) {
                console.log(err);
            } else {
                pm.test("Status code is 200", () => {
                    pm.expect(res).to.have.status(200);
                });
                let resJson = res.json();
                pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_in);
                pm.collectionVariables.set("bearerToken", resJson.id_token);
            }
        });
    }
});

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