Hello everyone!
Postman is one of the most popular API testing tools - it combines the ability to test things manually and add some sort of automation to it. In this article you’ll know how to best organize your Workspace in Postman - it’ll help you faster reach any request your need and get rid of some manual tasks like renewing your authorization token.
So let’s begin with the creation of a new Workspace!
Usually, there are at least a few environments in most of the projects - development (where developers upload their features first and might test them on their end), staging (platform for QA verification) and production, of couse.
Every environment has a bit different base url, but path (route) usually stays the same. For example:
See? In case of staging and development environments - they simple have subdomains assigned, but the path (some/route) stays the same. It means that base url is one of our first candidates to become an environment variable - a value that changes between environments.
Usage of base url as an environment variable let us use a single template in Postman to send a request to any environment that we have configured in our collection.
Now let’s create a new Environment and name it Production. These are the variables that we’re going to add:
And don’t forget to click Save button to save our current progress with the Production environment.
Now it’s time to create a Collection to organize our templates - it is as simple as our previous step with environment variables. Let’s name new collection Paysis and select Bearer Token as an Authorization Type since this is the one that current application use. Below, in Token section, enter baseUrl environment variable (don’t forget to put parentheses as well - that’s how you address to environment variables in Postman). After that select the Environment that we created in our previous step.
Once again, don’t forget to Save current collection - either press Control + S (Windows / Linux) / Command + S (MacOS) or click View more actions (three dots) button.
Our Authorization config will be applied for every request in the current Collection automatically.
Let’s create our first request - this one is going to be an authorization request which makes our further testing possible (since we need an authorization token to send other requests as an authorized user).
To create a new request template - simply click right mouse button on your Collection name in list and select Add Request. Let’s name this one Auth.
It is a POST request to endpoint /auth with JSON body:
{
"login": "{{login}}",
"password": "{{password}}"
}
baseUrl, login and password are environment variables there that are taken from Production environment that we’ve created earlier.
There’s one thing that we’d want to automate here - update token environment variable with a value from this request’s response, so we can reuse this value in other requests that must be executed under authorized user. To do that, let’s go to Tests tab in current request and paste this code there:
var responseData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", responseData.token);
This section is there for small automated tests and scripts (just like the one that we already implemented). It accepts JavaScript only. Once you’ll send a request - code from this section will be executed.
By the way, since this request doesn’t require any authentication header - we must remove it (because it was automatically applied by collection). To do that - simply go to Authorization tab of current request and select No Auth in the dropdown.
Now it’s safe to execute our first request in the collection - hit Send button.
Once you’ve done that - you’ll see authorization token from Auth response in your current environment. This variable with your token will be included by default in your future requests of the current collection.
After some time you’ll see that your collection is growing and it’s hard to find a request that you need in this mess. It’s better to separate requests by groups (sub-collections of collections) right from the start.
For Paysis, we have Auth request in the root of the collection. For other logic (Users, Transactions, Config) there are sub-collections.
Don’t forget about proper naming of requests so other people could find whatever they need as well.
So here’s what you need for the best experience with Postman:
Thanks for reading, I hope you’ve learned something new!