Pass token from response to another request

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. Is this an Authorization token you are storing in you envionment file?

I have something very similar. I have a step where I create an “External Identity token” Bearer Token, which I write to a collection variable

pm.collectionVariables.set("externalIdentityToken", pm.response.json().access_token);

Then in a subsequent step this token is called in the Header tab:

If this is not your process, maybe you could provide some more details about exactly what you are trying to do?

Hi @adam.allford

Yes it is a Auth token and it’s the same scenario as yours. I put it in a environment variable and then that variable is read from header tab.

Are you able to run your requests using the collection runner? Or are you running them manually. Mine works fine when I run manually but not when I run from the runner.

Where do you have this - Is it in the tests section in the 1st request?

If that’s the case, then I think the problem is the execution order of the collection runner - it runs request 2 before it runs the test script in request 1, so request 2 will not have the auth token.

@defconn Yes that set collection Variable is in the Test tab on the first request.

If the issue is just through the collection runner then you just need to re-order the requests in the collection (drag and drop) and it should then run them in the order they are set up.

@adam.allford

The requests are ordered correctly, this is how it would be executed when you run it manually
Request 1
Test script in Request 1
Request 2

but when you run through the collection, Postman documentation says this will be order:

Request 1
Request 2
Test script in request 1

@defconn
That is strange. Whenever I run a collection through the test runner, the tests are executed in the order they are displayed in the collection. So if I had a collection with 2 requests with associated tests it would run - Request 1 with the test script, request 2 with test script.

Could you post a screenshot of you collection and the results of the test runner?

Do you have any variables or tests on the collection level?

Most of my variables are stored at the collection level (apart from Environment specific ones)
Some of my tests are at a folder level (where the same response is expected for each request), or against a specifc request (where the response is unique)

Are you able to share your collection, where you have an auth response from a request being passed to another request inside a collection. I can then see where im going wrong. Just a simple collection with 2 requests will do.

I can’t share the collection, but I can show you how I have it set-up
image

So my Test collection has a set-up folder, which will create the token and a TransactionId, all of which are stored as collection variables.

Submitting an order then uses the Auth Token saved to the collection:

And the transactionID is used in the body of the order submission:
image

That is it. I create tokens/values and save them as collection variables and then use those variable to use the values when they are needed in the test run. So long as the requests and calls are in the correct order in the collection it will work (unless there is a further underlying system issue)