Skip request based on condition (collection)

I am trying to run though my API calls yet only trigger the GET requests and save the response body to a file. If you have any way of achieving this please let me know.

It would be really nice to be able to choose whether a request runs based on a condition. Right now it seems this cannot be done. Pre-request scripts are too late in the picture. Even something simple like pm.request.method ne GET.

Simple conditions should not be hard to add to the UI. Drop down of pm vars followed by a drop down of conditional expression and then a user supplied value or boolean.

You can use postman.setNextRequest() to skip a request , if you set postman.setNextRequest(null) it wont execute the next request.

You can also try skipping a request to the server by setting pm.request.url=""

1 Like

Hi @rtfmoz2!
If you are using Runner to run through the requests in a collection, you can select requests to run by checking/unchecking the box as shown below:

Alternatively, pm.sendRequest() method may be useful to send request from scripts. As in the example below, you can define request as an object and use it as an argument.

// Example with a full-fledged request
const postRequest = {
  url: 'https://postman-echo.com/post',
  method: 'POST',
  header: {
    'Content-Type': 'application/json',
    'X-Foo': 'bar'
  },
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};
pm.sendRequest(postRequest, (error, response) => {
  console.log(error ? error : response.json());
});

I hope this helps.
Please let me know if you have a further question:)

I agree with what @praveendvd said. What you could possible do is create a set of environment variables for which types of requests you want to run.
image
In your pre-request script (you could even do this at the collection level so you didn’t have to repeat it for every request) you can use the following script:

if(pm.request.method === 'GET' && !pm.environment.get('executeGETRequests') {
  pm.request.url = 'https://www.postman-echo.com/delay/0';
} 
else if(pm.request.method === 'PUT' && !pm.environment.get('executePUTRequests') {
  pm.request.url = 'https://www.postman-echo.com/delay/0';
  pm.request.method = 'GET';
}
else if(pm.request.method === 'POST' && !pm.environment.get('executePOSTRequests') {
  pm.request.url = 'https://www.postman-echo.com/delay/0';
  pm.request.method = 'GET';
}
else if(pm.request.method === 'DELETE' && !pm.environment.get('executeDELETERequests') {
  pm.request.url = 'https://www.postman-echo.com/delay/0';
  pm.request.method = 'GET';
}

This script will check if the request is a specific HTTP method, check your environment variable to see if it should execute, and if it’s not allowed, change the request to hit the postman echo API - which is essentially a no-op. It does not permanently change your request, just that specific execution.

You can’t have the condition in the request you want to skip, as you would not evaluate the condition.
But in previous request you can evaluate condition and if not met say: postman.setNextRequest(“put request that goes after the one you skip”
so in request 1 you say if whatever is false then "postman.setNextRequest(“request3” and effectivelly you are skipping request2. This is not theoretical I use it in one collection and works very well. You can use avariable for whatever you may need in other calls if skipping has side effects, put a message for collection output and decide what is your next step if condition not met, I skip 2 requests in my case.

if (condition_here )
{
postman.setEnvironmentVariable("flagVar", 0);
}
else  
    {
    tests["Just a message to output"] = true;
    postman.setEnvironmentVariable("flagVar", 1);
    postman.setNextRequest("request after the one I skip")
    }

I figured out putting logic in the test criteria gave the best outcome

// Environment checks prior to begin testing.
stop = true;
pm.test("Testing Environment", () => {
    // Verify code is on the platform
    // Using response_type none to generate expected outcome... 
    // Only works if client config has been deployed.
    pm.response.to.have.status(302);
    pm.response.to.have.header("Location");
    pm.expect(pm.response.headers.get("Location")).to.include("unsupported_response_type");

    stop = false; // only reaches this command if all tests have passed
});

if (stop) {
    postman.setNextRequest(null) // don't process anymore requests
}

The same command setNextRequest, can be used to skip to a later request by name I also found out. Should have come back here days ago…

2 Likes

As it turns out after more experience with postman the stop-true logic is useful only when you have multiple pm.test() calls one after the other. But calling setNextRequest(null) is just as effective. It would be nice to wrap these things up in functions stored at the folder or collection level but it’s just not possible to do so for individual request testing as it does not include those values when you execute a single request.

I did exactly that…

pre-request

test on request

@rtfmoz2

Where did you setup those reusable functions and are they reusable within the entire collection? I’ve always thought it’d be nice to have some kind of library of functions to use for setting things up in pre-request scripts and grabbing data from results, but didn’t think it was possible (or at least possible to do what I think I wanted).

Thanks!