Pass response to another request in collection

Hi Guys,

Newbie here, I’m creating a test suite for a collection that has multiple requests, the first request within the collection will return a token which im storing in an environment variable within a test in request 1. I would like this token to get passed to request 2 which needs this token but I cant get it to work from running in collection folder.

Hi @defconn

In your first request, you would need to set the variable in the “Test” tab using;
pm.environment.set("variable_key", "variable_value");

Then in your second request, you would call the variable using the {{}} notation.

Here is an example;
First call;

Second call;

Hi @w4dd325 , your suggestion is exactly what I did, the problem is when I run the requests from the collection test runner.

I think the problem is the execution of the collection test runner, it will run request 2 before it runs the test script in request 1 (which saves the token from response 1) which means request 1 passes but request 2 doesnt have the token yet so it fails.

That was run in the collection runner.

If your issue is simply the flow of requests being made then try using the setNextRequest option. It will allow you to explicitly state in which order calls are executed.

The requests are ordered correctly, but how can I run test script in request 1 run before request 2? It doesnt work if I put it in the pre-request.

Can you show what you have tried?

Hello,
I just want to take this time to share an example collection that uses this type of workflow

https://www.postman.com/postman/workspace/published-postman-templates/folder/20021534-f2b39eb1-393a-4a9e-8fb0-135abb60c9d3?ctx=documentation

If you take a look at folder Using Request Workflows you can see how the postman.setNextRequest() is utilized. Feel free to fork and add the right authorization to run this folder using the collection runner

I noticed that you have recommended storing the collection request token at environment level.
Given that you have only mentioned use of that token in the collection, it may be better practice to store the token in the collection (pm.collectionVariables.get/set) rather than the environment (pm.environment.get/set).

@bryan-fisk

It makes little difference in whether its a collection or environment variable.

You retrieve in the same way. The advantage of using environment variables is that you can set them to be secret (which means the info isn’t copied to the Postman cloud or your CI tool).

Just ensure you don’t have it set in both place so you don’t get scope issues.

On a side note, I’m a bit confused by the original issue.

Normally requests run in order. Request 1 should always run before request 2.

The token in request 1 needs to be added to the variable using the Tests tab (after the request has run). Not the pre-request script.

You use setNextRequest when you want it to go in a different order or loop through requests.

You can also use sendRequest as a pre-request script to get the token, which means this is all in one request. (Although I would just use the two as its easier to debug\support).

FYI: According to Introducing the Secret Variable Type in Postman the secret type is only to prevent revealing the value by masking it in the UI (i.e. prevents over-the-shoulder type reveal of the value unless you click the ‘eye’ button to reveal it).
The value is still saved to Postman Cloud and the exported Environment contains the Initial Value of the secret **.
** I have tested this by creating a secret variable in an Environment, logging out of my account (which clears local cache) and exporting an environment . Neither of these actions result in the secret value being unavailable.

@bryan-fisk

How to Use API Keys in Postman | Postman Blog

The current value is local to your session within your Postman app. (I don’t use the initial value).

1 Like