paint-brush
Get Your Postman Workspace Organizedby@bormando
1,307 reads
1,307 reads

Get Your Postman Workspace Organized

by Dmitrii BormotovJanuary 24th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Postman allows you organize workspace so it's easy find and execute API requests with parameters that you need. Environment variables will help you adapt your requests to various test environments and make them reusable. Some scripts will also make your life easier, especially if they do for you the work that you were doing manually before. One of such scripts is the one that saves authorization token from auth request into environment variable. Use collections and proper naming to not to get lost in your workspace as number of requests will grow eventually.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Get Your Postman Workspace Organized
Dmitrii Bormotov HackerNoon profile picture


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!

Creation of a new Workspace in Postman

Environment

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:

  • baseUrl
  • token
    • authorization token - it’ll be updated dynamically, so it must be empty for now
  • login
    • value - adminius
    • login for auth request
  • password
    • value - supers3cret
    • password for auth request

Environment creation in Postman

And don’t forget to click Save button to save our current progress with the Production environment.

Collection

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.

Collection creation in PostmanOnce 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.

Authorization

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. Request creation under Collection

It is a POST request to endpoint /auth with JSON body:

{
  "login": "{{login}}",
  "password": "{{password}}"
}

Setting request detailsbaseUrl, 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);

Scripts in PostmanThis 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.


Authorization method selection

Now it’s safe to execute our first request in the collection - hit Send button.

Sending the request


Once you’ve done that - you’ll see authorization token from Auth response in your current environment. Updated authorization tokenThis variable with your token will be included by default in your future requests of the current collection.

Functional Separation of Requests

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.

Paysis collection example

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.

Summary

So here’s what you need for the best experience with Postman:

  1. Use environment variables in your workspace to adapt your requests to various environment.
  2. Add scripts that automate data transitions between requests (like sharing authorization token from Auth request into all other requests).
  3. Create sub-collections and name your requests properly.


Thanks for reading, I hope you’ve learned something new!