codingguy3000
codingguy3000

Reputation: 2835

Postman Global pre-request script that all collections inherit

Postman is probably the most amazing piece of software I've ever encountered in my software development career.

However, there is something I'm stuck on and I believe there is an answer out there...

I have a huge pre-request script that I copy to each new postman collection that I create. The pre-request script does different things including setting the server to run my request on, generating reference numbers, and many other tasks.

The problem is that I have the to copy this code all over the place. Each Collection I create gets the same blob of code. And then as time moves forward I update my blob of code and then forget which collection has the latest updates.

I was told that it's possible to set up a global pre-request script in Postman that all collection will execute. I've spent some time searching the internet and I can't find the answer.

Any help would be greatly appreciated...

Upvotes: 6

Views: 2900

Answers (3)

Radu M
Radu M

Reputation: 1220

I think you can't do it over more "real collections" without any custom shell script.

If it would be possible, I think they would mention it here. https://learning.postman.com/docs/writing-scripts/pre-request-scripts/

Postman supports from itself only one Pre-request Script for each "real collection" - but you could mimic "sub collections" of one upper collection by doing folders under the "real collection".

So the real collection would be my-server-collection - this one contains your pre-request script and every Rest API Controller is a subfolder under this collection - so you would get the same effect.

screenshot

Upvotes: 3

devblack.exe
devblack.exe

Reputation: 584

You go to the Environments tab and click on Create new Environment icon.

In the Environment windows add a name to it and a variable for your pre-request script. e.g. preRequestScript and set the value to your pre-request code. (Save it of course)

Lastly, you can go to your Collections, edit the one you want, and select the "Global" environment you created from the dropdown.

Once you finish, the global pre-request script will run before each request in your selected collection.

Upvotes: 0

bitoiu
bitoiu

Reputation: 7484

I was told that it's possible to set up a global pre-request script in Postman that all collection will execute. I've spent some time searching the internet and I can't find the answer.

Did this come from Postman itself? I'm pretty sure Collection webhooks are set per collection, as this is a topic I've explored in depth before. I went to check just in case you could skip naming a collection to force it to * or something, but nope:

enter image description here

With that out of the day, the only suggestion I have to you is to create an utility collection that traverses all collections with a given convention name, for example PRS-X, PRS-Y. For those collections your utility would edit each collection to add/update the pre-request script.

As you probably know you could run that on-demand, schedule it, or initiate the run other automation (like an update to your pre-request script).

Upvotes: 1

Related Questions