I have TOKEN that a collection environment in Postman. I use it to set Authentication for all requests in this collection.
The flow goes like this:
- Send
loginrequest from Postman - Retrieve the response and copy
access_tokenvalue from the response body - Go to the collection environment manager
- Set
TOKENvariable value - Submit the other requests
I can see that Tests execute after the response is received. When you select Send, Postman runs your test script after the response data returns from the API so I use it to set TOKEN variable automatically.
I write this script for login request:
var jsonData = JSON.parse(responseBody);
pm.collectionVariables.set("TOKEN", `${jsonData.data.access_token}`);
1
2
2
After login success, I can see the TOKEN variable value has changed, so I can make the other requests.
Refs #
- https://learning.postman.com/docs/writing-scripts/test-scripts/#writing-test-scripts
Comment