I scoured the docs and the internet to see if this is possible but was unable to find anything. Is there a way to use an existing Postman request that’s defined within the same collection or different collection in a pre/post script? I know there’s a pm.sendRequest function, but it just strikes me as inefficient to have to define the request again.
I think what you be looking for is the setNextRequest method. You can use this to programmatically set the next request based on your own conditional logic in the Pre-request or Tests tab. Here’s a bit more information on controlling your workflow within Postman.
Thanks @sean.keegan for the reference and the prompt response.
I looked at the docs and it looks like it sets the workflow and you can’t make use of the response in a pre-script. My use case here is that I have to auth against a login endpoint and then use the generated token as in an auth header for another request. I have to do this for all requests for a particular REST resource. I would like to just automate by defining a collection pre script that is executed before each call, calls the auth endpoint, grabs the token and sets the token as a collection ENV var. My other requests would then use that ENV var to set its auth headers.
It’s not a big deal if it’s not possible as I can just accomplish this through the sendRequest function.
Ah okay! I think what you may want to do is take advantage of the pre-request script at the collection level then. Anything in the pre-request script of the collection level folder will run before each and every request in the collection.
If I’m understanding your need and use case correctly, one of our Postman community superstars @praveendvd has demonstrated something similar to this in his public workspace. Check it out here and see if this is something that might be useful for what you’re looking for - specifically look at the pre-request tab of the collection level folder.
Aside from running your folder via the collection runner in Postman, you can chain these requests with postman.setNextRequest("collection name or ID") individually.
Postman does not otherwise support running an entire folder programmatically in this way.