0

I want to chain some requests in order to always have an up to date variable containing a JWT for access.

Now i am new to scripting in Postman, i figured this can be done in pre-request scripts. There i can access (global / environment) variables, but i wonder can i also access a saved request from a collection in a script? The saved request has body and header set, partially with environment vars.

Environment and collection variables can be accessed like this, i know:

pm.environment.get('some_var')
pm.collectionVariables.get('name')

This is the snippet provided by Postman

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

Is there a way so i can do something like

pm.collection.url.get('obtain_jwt')

if i have a request called 'obtain_jwt' in a collection? Or do i have to store the URL as a variable as well and again fill body and header in the script? (which i want to avoid)

1 Answers1

1

I don't think it's supported in Postman the way that you're thinking, it's a feature that's been an ask for a long time:

https://github.com/postmanlabs/postman-app-support/issues/4193

You'll need to fully construct the request in the pre-request script or you might be able to leverage setNextRequest

so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50
  • Thank you so much. Yes it appears this has not been implemented, and the request (haha) to add this feature exists for quite a while already. Will check `setNextRequest`! Otherwise store the URL to my 'login' endpoint in a var, and manually set header and body. Then copy the whole scripted request to every request where it should be run in advance. Hoping i will never have to edit the scripted request. – anotherFishInTheTank May 24 '22 at 07:33
  • Yup that's what I've had to do in my collections and it can be really annoying like you said if it ever changes and then you have to remember to update every script :/ – so cal cheesehead May 24 '22 at 17:34