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.

I have a similar scenario, and what I do is build my full list of items from an array stored in an env variable , and then filter it based on some criteria, and make a call for each one of these from the pre-test tab using a function that does a sendRequest call and logs the responseBody to a env variable per request. These requests all fire off at once and log results. You can’t interact with the results on the pre-test tab because they’re called async.

Then in the Test tab I read my array again from env variable, do my filter and use that to know what env variables I made (each element has an ID and i use the ID to name the variable where the response body was saved.) I do a new loop and call a function to process the data, and do a pm.environment.unset(“IDn”) on each loop, to clean things up.

So, the actual call being made is not really meaningful, it is just what is used to allow for triggering pre and post calls.

1 Like

Thank you for the excellent suggestion jonsworkalias! This will be a good solution for much of what I am looking to do. For those request body’s that I can’t dynamically set to my knowledge (issue with x-www-form-urlencoded), I’ll just used a preformed one that I can substitute the variables in. Thx!

1 Like