# Stop request from sending if pre-request test fails

**URL:** <https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066>\
**Category:** Help Hub\
**Tags:** tests, pre-request\
**Created:** [July 12, 2018, 9:15am UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066 "2018-07-12T09:15:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocovan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/cocovan/32/742_2.png) [@cocovan](https://community.postman.com/u/cocovan)\
**Post date:** [July 12, 2018, 9:15am UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/1 "2018-07-12T09:15:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 12, 2018, 9:31am UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/2 "2018-07-12T09:31:04Z")

</div>

@cocovan See [Controlling the execution of current request in pre-request script](https://community.postman.com/t/controlling-the-execution-of-current-request-in-pre-request-script/2053)

---

<div class="post-metadata">

**Author:** ![cocovan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/cocovan/32/742_2.png) [@cocovan](https://community.postman.com/u/cocovan)\
**Post date:** [July 12, 2018, 9:44am UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/3 "2018-07-12T09:44:07Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![tkloeber](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/tkloeber/32/8884_2.png) [@tkloeber](https://community.postman.com/u/tkloeber)\
**Post date:** [August 31, 2018, 8:25am UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/4 "2018-08-31T08:25:18Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![whatafuck](https://avatars.discourse-cdn.com/v4/letter/w/f0a364/32.png) [@whatafuck](https://community.postman.com/u/whatafuck)\
**Post date:** [May 29, 2019, 2:37pm UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/5 "2019-05-29T14:37:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gabouchet](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gabouchet/32/9737_2.png) [@Gabouchet](https://community.postman.com/u/Gabouchet)\
**Post date:** [September 9, 2021, 1:21pm UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/6 "2021-09-09T13:21:42Z")

</div>

It’s work, thanks guy ! 🙏

---

<div class="post-metadata">

**Author:** ![ganeshkaki](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@ganeshkaki](https://community.postman.com/u/ganeshkaki)\
**Post date:** [April 13, 2022, 9:02pm UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/7 "2022-04-13T21:02:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![amprati-eai](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@amprati-eai](https://community.postman.com/u/amprati-eai)\
**Post date:** [May 18, 2022, 4:36pm UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/8 "2022-05-18T16:36:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mission-technologis9](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mission-technologis9](https://community.postman.com/u/mission-technologis9)\
**Post date:** [June 15, 2023, 5:09pm UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/9 "2023-06-15T17:09:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shawnsmith](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@shawnsmith](https://community.postman.com/u/shawnsmith)\
**Post date:** [September 20, 2023, 12:09am UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/10 "2023-09-20T00:09:40Z")

</div>

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”);  
}

---

<div class="post-metadata">

**Author:** ![Whersh](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@Whersh](https://community.postman.com/u/Whersh)\
**Post date:** [November 3, 2023, 4:03pm UTC](https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/2066/11 "2023-11-03T16:03:47Z")

</div>

It looks like we are going to get this soon

> <https://github.com/postmanlabs/postman-app-support/issues/8929#issuecomment-1790189424>
>
> In many other softwares pre-request script usually can stop continuing main oper…ation. Such as in git pre-commit script we can stop commit operation if we want, what is expected. But in postman pre-request script we can not stop request which is illogical. So postman pre-request script does not use its potential. I have found several years old fallback by throwing error but it is not good solution because in collection runner there are many red lines.
> 
> I have data file with e.g. 30 iterations but I do now want to run each iteration for each request. Some request are simple so I can not write 30 alternatives of variable value. SO I would like to add conditions in some requests... e.g. pseudo code... 
> \`if (pm.info.iteration \> 10)
> pm.request.stop() // or pm.request.skip()\`
> 
> Is it hard to add this feature? Is it planned samewhen?

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