I've noticed when testing authenticated endpoints in Postman that every time my token expired, I needed to take a new token from my POST /authenticate endpoint and copy-and-paste it into each of my requests. This can quickly become a tedious process when your token's TTL is very short.
So I came across a quick-fix that has helped me save time by completing this process in just one click, and I wanted to share it.
The Solution
1) Start using environment variables!
Rather than explicitly entering the value into fields, take advantage of Postman's environment variables. There are different levels of variables (see here, but I like to keep my projects organized by creating different environments.
You can even set up for different environments (i.e. dev, sandbox, prod)
To add to a field, just surround the variable name with double curly brackets: {{token}}
.
Now all of your variables live in one place.
2) Create a post-request script
Now you can set the environment variable using a post-request script. Under the endpoint tab where you get your token (usually a POST to an authentication endpoint), click the "Tests" tab.
This is where you can do all kinds of things (check out the snippets on the right side). However, in this case, we are just setting an environment variable.
Add this script to the panel:
const token = pm.response.json().token;
pm.environment.set("token", token);
Note: Your authentication endpoint response may have a different structure so make sure in Line 1 you are referencing the correct token property.
Now any time your token expires, you just call this endpoint and the token returned will be used everywhere it is referenced (i.e. all your protected endpoints).
Conclusion
I understand that this probably isn't the best way to do this. Please share a better solution in the comments! It would be extremely helpful for everyone.
Also, I know that I never use Postman to its full potential, so if you have any useful tips on Postman in general, please share those as well!