One of the requests resets my auth token and the other fetches some data needed for the main request I want to send.
The issue is that the data fetching request also needs that auth token to be successful.
Can I write my pre-request script in a way so that my data fetching request always runs AFTER my auth token request?
both req1 and req2 are sent out immediately without waiting for them to complete. In order to make req2 wait for req1 you have to nest pm.sendRequest(req2) inside the callback of req1. It will be something like,
pm.sendRequest(req1, function () {
pm.sendRequest(req2, done);
});
Although this solves your problem, it can get unreadable and complex over time. I’m interested in finding alternatives to solve this. Have you tried seeting the three requests in order in a collection and running them?
Thanks for the nested solution. Agreed, it might be quickly unreadable, unless perhaps if the nested request is done with the code in a variable, e.g.:
eval(pm.globals.get("customerLogin"))();
And yes, I have been using separate requests organised in collections and folders but I quickly reached in excess of 1800 requests for a single API so I’ve been trying to find ways to reduce that number. Including some requests in the pre-request scripts, especially authentication related ones, helps making things a bit easier to navigate through.
I have been using separate requests organised in collections and folders but I quickly reached in excess of 1800 requests for a single API so I’ve been trying to find ways to reduce that number. Including some requests in the pre-request scripts, especially authentication related ones, helps making things a bit easier to navigate through.
We’ve been working on something that might help with that. We are introducing scripts and auth at collection/folder level. It is available for early access on our Canary channel(for linux and mac, coming out on windows soon).
We’ve added support for collection and folder level scripts and auth in our stable channel. Please update to the latest version and try it out. You can find the option to add scripts and auth in the Edit Folder/Collection option.
Nope. pm.sendRequests are asynchronous always. However, you will have the option to group common scripts in folder/collection scripts in the latest version.
Yes it is possible! For instance, I use this for retrieve a JS archive from a CDN, then make it available before the collection gets executed. So paste some similar snippet into your Pre-request Scripts:
That is not a valid Request object to pass to the pm.sendRequest function. The Request object is docmented here: http://www.postmanlabs.com/postman-collection/Request.html, and it has neither “async” nor “crossDomain” properties. In short, you can’t do synchronous calls this way.
You are probably right. However, it s working for me so far! Magic? Older version? Which to.hear more details to have a deep understanding. Thanks for feedback
Older version, maybe. Trust me, I wanted that to work in the worst way!
For anyone else with this kind of need who stumbles across this thread, the way I ended up solving my particular use case was to break my request into two separate requests. In my case, the first request performs the API operation and the second fetches data from Couchbase to validate against. I attach the actual tests for the FIRST request to the test block of the SECOND request. I don’t use sendRequest at all, now.
In the OPs case, I would make the call to get the auth key from the first request and stash it in an environment variable, and then do the API request & associated tests in the second request.
to be consistent with the postman workflow, you should have a pivot request with the aim of synchronously to call many times the other requests, the pivot could have a simple request like “postman-echo.com/get” in order to build the next request on the test script in Postman, the above must set the index value of the iteration to avoid infinity cycle, as show the next image
the calculateFunds post will execute many times through of pivot request, and finally the calculateFunds test script will have to call pivot and eventually using the postman workflow, you don’t mind about the asynchronous callings.