How Can One Terminate Executing a Request From Within Pre-Request Script?

When I go to execute a request, I use logic within the Pre-request Script to determine IF I really want to execute this request or not based on environment variable settings. I don’t seem to be able to find a way of specifically terminating execution prior to the invocation of the actual API call and subsequent Tests scripts. In short, that is what I want to do, stop prior to the actual API call.

I tried using postman.setNextRequest(null) but it doesn’t stop execution at that time and still goes thru executing the API call and subsequent Tests script. I confirmed that this is the expected functionality of postman.setNextRequest. So is there another way to effectively abort execution and move on to the next request?

One method I used was to make an environment variable out of the Method call and control its setting within the pre-request script. For example, I set the Method to {{TEMPMETHOD}}. If I determined in the pre-request script that I wanted to execute this test, I set the variable to the desired method (i.e. GET, POST). If I determined that I wanted to skip this request, I set the variable to the value “OPTIONS”. This still executes my API call but only in what I will call inquiry mode, returning in the headers a listing all the acceptable Method options (i.e. POST, GET, PUT…).

Now I need to continue the charade on into the Tests scripts because there are assertions I want to perform IF it is a real request vs an OPTIONS Method request. So I added logic within to conditionally execute the assertions based on the value of the variable {{TEMPMETHOD}}.

While this does seem to work around my issue, I am concerned that it is not sustainable for all circumstances, furthermore, it requires me to make my requests have a Method that is a variable. Can anyone think of a better way to control execution of a request to the point where I can exit out prior to actually invoking the API call itself?

And for full disclosure, I actually am looping on the same request with multiple sets of data within the request itself. So I have an array of request data within the pre-request, as well as an array of tests to perform. I then filter the request data based on some some criteria. For example, I may want to perform execution on rows where the column “dataCountryCode” is set to “US” or “CA”. I end up with a “filtered array” containing the header and all the rows whose column dataCountryCode is set to US or CA. I then iterate thru the array of filtered request data and if there is another row of data to exercise, I perform a postman.setNextRequest(request.name) to loop back onto myself and grab the next row of data to process. My original issue I noted above (wanting to stop execution prior to the API call itself) ONLY COMES INTO EFFECT should my filtered array consist of no data rows found so I don’t want to process the request at all. What I just said in this paragraph should not be related directly to my request/issue. One could work with data files instead but as far as I can tell, there are limitations there too and in the end, I’d still want to filter on a given column and could end up with no rows to process. I just wanted to provide some additional insight as to why I may want to abort/skip a request once started and prior to the actual API call being made itself.

A hacky way to do what you’re trying to do is to add a request that doesn’t really do anything (think Postman Echo) then do your setNextRequest logic in the tests of that request. That way you would be able to skip your API. If I understand your message your flow iterates over the same request:

  1. SLaBoss’s API Request
    a. Logic to verify if API should run as pre-request script
    b. Request is sent
    c. Assertions and setNextRequest(SLaBoss’s API Request)

What I am proposing looks slightly different

  1. Postman Echo
    a. Request is sent
    b. Logic to verify if SLaBoss’s API should run as test script
  2. SLaBoss’s API Request
    a. Request is sent
    b. Assertions and setNextRequest(Postman Echo)

It adds a superfluous request, but I think it achieves what you’re looking for.

Thanks Allen. I appreciate your thinking out of the box. I think I am trading one hack for another. With my approach it all stays self-contained within one request. With the proposed two request approach, I need to track inputs and desired
outputs across requests, as well as remembering what the “previous” echo request name was to loop back to.