# Postman - re-use same request for testing?

**URL:** <https://community.postman.com/t/postman-re-use-same-request-for-testing/22455>\
**Category:** Help Hub\
**Tags:** tests, postman\
**Created:** [April 9, 2021, 2:45pm UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455 "2021-04-09T14:45:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thim](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/thim/32/4877_2.png) [@thim](https://community.postman.com/u/thim)\
**Post date:** [April 9, 2021, 2:45pm UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/1 "2021-04-09T14:45:26Z")

</div>

In Postman you can define multiple response examples.

200 \> success  
404 \> not found

In the postman collection, the examples are added but I cannot execute the request in Postman to return a 404.

I want to be able to run a request 2 times:

- happy path: run the request and test if the path returns a 200
- unhappy path: run the same request, but for example use a blank request parameter and test for a 404

Is there a way to do this natively in Postman?  
Or is the only option to duplicate the request and manually modify the request to make the parameter blanc and test for a 404?

---

<div class="post-metadata">

**Author:** ![sean.keegan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sean.keegan/32/6353_2.png) [@sean.keegan](https://community.postman.com/u/sean.keegan)\
**Post date:** [April 9, 2021, 3:26pm UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/2 "2021-04-09T15:26:54Z")

</div>

Hi @thim! I’m not sure if I’m fully understanding your question, but here’s a method that may be helpful for you:

[removeQueryParams](https://www.postmanlabs.com/postman-collection/Request.html#removeQueryParams) allows you to programmatically remove a query param from a search. You can add in some conditional logic in your Pre-request script to determine whether or not to include a query parameter.

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [April 9, 2021, 5:12pm UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/3 "2021-04-09T17:12:54Z")

</div>

You may want to look into data-driven testing.

Essentially, you reuse the same request but use a different data set for each execution. The external data file needs to be in CSV or JSON.

Here is a tutorial that can help you get started:

[![](https://img.youtube.com/vi/fr7UpFNQbLw/maxresdefault.jpg "Data-driven testing using Postman") ](https://www.youtube.com/watch?v=fr7UpFNQbLw)

You can define expected data in your external fine and use that in your assertions.

Something like:

```
pm.test("Status code", () => {
    pm.response.to.have.status(pm.variables.get('expectedStatusCode'));
});

```

---

<div class="post-metadata">

**Author:** ![thim](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/thim/32/4877_2.png) [@thim](https://community.postman.com/u/thim)\
**Post date:** [April 11, 2021, 9:53pm UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/4 "2021-04-11T21:53:19Z")

</div>

I’m looking more for a different flow

Execute a request \> validate for 200 response  
Next remove a paramater from the same request \> Execute the same request \> validate for 404 response.

I’m not sure how the data-driven flow via a CSV/JSON file would facilitate this?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [April 12, 2021, 4:42am UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/5 "2021-04-12T04:42:19Z")

</div>

Hello @thim, If I get your requirement correctly,

You need to test a same request for positive and negative flows (test case should pass though).

You can try a simple if else statement with the response Codes. Also I believe you are trying to do get 404 by removing the params. In that case,

**Step 1:** Make sure to parameterize the parameters of the request.  
Step 2: Under your tests section you can write a if else logic like below:

```auto
if (pm.response.code === 200)
{ pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
        pm.environment.set("pathparam1","dummy");
    });
    }
else if (pm.response.code === 404){
pm.test("Status code is 404", function () {
    pm.response.to.have.status(404);
    pm.environment.set("pathparam1","pet");
    }); 
}

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/8/c8c9025fff02722a944b1c25c3fc64d3a2494212.png)

You can customize it based on your needs 😊

---

<div class="post-metadata">

**Author:** ![thim](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/thim/32/4877_2.png) [@thim](https://community.postman.com/u/thim)\
**Post date:** [April 12, 2021, 2:29pm UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/6 "2021-04-12T14:29:09Z")

</div>

@bpricilla I think I understand properly.

How do you rerun the same request? Would this be done by the newman iterations option?  
And how do you know when to stop rerunning the samen request?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [April 13, 2021, 6:14am UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/7 "2021-04-13T06:14:48Z")

</div>

Hi @thim, I just clicked send twice. Since you said single request for validating two response codes. Based on your need you can tweak it 😊 It’s a rough idea based on my logic 🙂

How are you running the remaining requests? And what’s the business usage for validating the same request for two different response codes?

---

<div class="post-metadata">

**Author:** ![sean.keegan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sean.keegan/32/6353_2.png) [@sean.keegan](https://community.postman.com/u/sean.keegan)\
**Post date:** [April 13, 2021, 1:46pm UTC](https://community.postman.com/t/postman-re-use-same-request-for-testing/22455/8 "2021-04-13T13:46:51Z")

</div>

@thim if you want to run requests in a collection in a specific order (or in your case, rerun the same request based on some status code conditions), you can do so using the `postman.setNextRequest()` method. [Here’s a helpful resource](https://learning.postman.com/docs/running-collections/building-workflows/) on building workflows.
