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.