Stop request from sending if pre-request test fails

Is there any way to prevent the request from sending due to a failed test in the pre-request script?

I’ve tried making the test a function and returning false, postman.setNextRequest(null) (which didn’t seem to even set the request to null anyway) and throwing an error. Nothing seemed to work.

Any ideas? Much appreciated. Iain

1 Like

@cocovan See Controlling the execution of current request in pre-request script

1 Like

@kunagpal - thanks. I’ve actually managed to get it to work - maybe not as gracefully as I wanted - by doing the following:

if (pm.variables.get("STR_ENTITY").toLowerCase() !== pm.variables.get("ENTITY").toLowerCase()) {
    console.error("Entity values do not match");
    throw new Error("Entity values do not match");
}

This throws an error for each iteration in the run so my users will need to go and look in the console to see what’s going on, but I’ve achieved the result I wanted.

2 Likes

@cocovan: this doesn’t work for me. My request is still sent even if the pre-request fails and throws the error. Anything else you did, to get it to work?

this doesn’t work for me also.
At postman UI - OK, with newman - NOK.

It’s work, thanks guy ! :pray:

During the runner, how we can continue with the next request.

If we are using throw new error… the runner not processing the next requests.

Please advise.

1 Like

It seems that now, upon a test fail in the pre-request phase, the default behaviour is preventing the postman request.

Hey @amprati-eai
How to prevent the postman request to pass if the postman pre request script fail?

I did not want to raise an error, if you also do not want to raise an error, then you can try this. I put the check in the preceding request’s tests. Pseudo code.
In Request2’s tests section
if var is x postman.setNextRequest(“Request3”);
else postman.setNextRequest(“Request4”);
In Request3’s and in Request4’s tests section, to reset things, I put
postman.setNextRequest();

Example code:
if($region === $uk) {
console.log("running the request, region: " + $region);
postman.setNextRequest(“NuReq”);
} else {
console.log("skipping this request: " + $region);
postman.setNextRequest(“StuffReq”);
}

It looks like we are going to get this soon

…pm.execution.skipRequest() . This function when used in Pre-request scripts will not execute the request…

1 Like