Postman is a popular API platform for building, testing, and iterating APIs. Most people who use the Postman to API test NEVER use the full power of this tool.
Why? — you can ask me. I’ll give you an answer:
Did you see people speaking on the phone in the Tesla instead of using a built-in hands-free device?
I don’t know why people don’t use the full power of the tools or devices, but I can slightly open for you the door to the magic world of the scripts in the Postman, my dear readers.
In the first post of the cycle of articles, we need to learn that Postman has a good tool — variables. The Postman has variables at different scopes, you can read about it here. From my practice, I’ve used only the following (I know that I wrote about a Tesla, but I can’t find a use for another):
– Collection variables — the variable that is available for all requests in the collection and independent of the environment. Be careful with this.
– Environment variables — if you have more than one environment, as I have, this variable will be your best friend in the Postman world.
Where I use variables:
- Request URL — the most used place for me. I use it in two ways:
– To replace a base URL. That is an effective way to have one collection for all your environments
– To set some data into the URL.
Example: I’ve created a user in one request and want to edit it in the following. I need to set the user id into the URL. You can do it with parameters or directly in the URL
- Headers — in most cases, headers change depending on the environment or if you have some specific header. The variables cover all my headers.
- Authorization — the authorization header in our testing can change every 3–5 minutes, depending on configuration, but… Who wants to copy and paste the JWT all the time when token updating? Nobody.
- Parameters — if I have nesting of the relationship in my URL structure, it’s always a head pain when I need to work with many parameters that change from request to request. Variables make it easy.
- Body — do you ever think about how to name the new user? Yes? Variables can do it for you. The attached screenshot shows that the built-in variables generated all the needed information.
- Pre-request scripts — from time to time, before we send the request, we need to do some manipulations. For example, when I changed the user email, I saved the old one to another variable. Example number two, I need to get some data from another request and save it to the variable.
- Tests — the best place where we can use variables. Why? Do you remember how long and hard to get the id of some entity in the ten-level nesting of the relationship? Variable can make it so simple. Write the variable once and forget about this trouble forever.
In the following article, I will discuss something interesting in the Postman world.