Postman - re-use same request for testing?

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?

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

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:

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

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?

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:

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

You can customize it based on your needs :blush:

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

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 :blush: It’s a rough idea based on my logic :slight_smile:

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

@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 on building workflows.