Automating Security token in Postman
Postman is a fantastic tool to quickly test your API(s). However, working in a microservice environment with end points being secured via a token system makes testing a PITA.
Adding a new security token every time the previous one expires slows us down, Here’s how to automate it —
- Latest version of Postman
- A service that is used by your application for authorization.
- Create a collection in Postman
2 . Go to Pre-request Scripts and add the following script —
3. Save the collection.
4. Create any request in this collection and this request will have the header inserted at run time.
How does the script work?
- We create the header for the request to the auth service.
- Then Extract the hostname from the request URL. Why?
If your dev env is split into multiple environments and with each env running its own auth service we need to hit the same host for both auth and actual request.
3. Create the request for the auth service.
4. Send the request. (Change the URL for auth)
5. Upsert the token the header. Change the header name accordingly.
That’s it. Test with ease now! 👍