Reuse existing Postman request including its Pre-request script and Test parts

Hello, I’m searching a way to repeat an already defined request in a collection when I use another request in order to not duplicate the request.

nextRequest feature doesn’t work for me in this case since this common request will be used in several request in the collection. Also not all of them will use it, so define it in folder level is not a solution for me.

The closest solution for me was this one: Use Existing Postman Request in Pre/Post Script - #5 by praveendvd. However the problem is that it doesn’t store the Pre-request script and Test part and there’re not executed.

The most important thing for me is the Test part since there I store data generated in the response of the request to be used in later requests, but that’s not the case. I don’t mind if the Pre-request part can’t be executed along with the repeated request.

1 Like

are you running data driven test or running the collection in one go ?

No, I don’t use data driven test for this situation. I run the collection using data randomly generated in certain fields.

So, no solution for this? Do you need more details for my problem?

I’m not sure what is the exact issue but from what I understood is that I think you need to send a specific request inside another based on some condition.
If my understanding of your problem is correct then you can use this solution. I have taken a sample url.

`pm.sendRequest({
        url: `${pm.environment.get("")}/api/documents?`,
        method: 'GET',
        auth: {
            type: "bearer",
            bearer: [
                {
                    key: "token",
                    value: "Bearer " + pm.environment.get("AccessToken"),
                    type: "string"
                }
	    ]
        },
        header: {
            'header': pm.environment.get("")
        }
    }, function (err, res) {
        if (err) {
            console.log(err);
        } else {
            pm.expect(res.code).to.be.equal(200);
            var response = res.json();
            console.log('response :', response);
            pm.expect(response.key).to.be.not.null;
            pm.environment.set('Id',response.id);
            pm.environment.set('FileId',response.File.id);
        }
    });`

Hello @jchaugule. Thanks for your reply. Maybe your solution is a bit convoluted. My question is to try to trigger an already defined request in a collection (including its Pre-request script and Test scripts) when it’s launched another request in the same collection.

To clarify, my case is something like this:

  • I have a test case that first launches Request A in a collection launched normally with a runner or Newman that creates an object. In its test part it’s stored the id of the object and other data.
  • The next requests work with the previous object created in Request A and finally the object is removed once the test case is finished.
  • Later in the same collection I want to launch another test case. To do it I need to launch Request B that needs object that is created in Request A. Instead of duplicate the request (including all its Pre-request scripts and Test scripts parts) I would like to ‘invoke’ Request A (including its Pre-request and Test scripts). This generates a new object so I need to store its data as before (id from Mongo database and so on).
  • Then I continue with other requests and finishes the test case.

The bold part is what I’m searching to achieve in order to avoid duplication of code and maintenance of all of this.