DEV Community

Cover image for My Postman
Mayank Yadav
Mayank Yadav

Posted on • Edited on

My Postman

Almost everyone who works into web development, front-end or back-end, has in some part of their development career used "Postman" or some other API client to test the app we are building. But, do we all know that Postman provides so much more than what we usually use it for.

Let's see how we can be more productive and use Postman to its full potential.

*Since this is not an introduction, I am assuming that everyone here is familiar with basic Postman features such as sending requests, saving requests, creating collections, blah blah blah. *

Time to deliver some letters. 😛

Environments and Variables:

Setting up environments can boost up your productivity if your application is deployed in different systems. With environments, we can switch between different versions in no time.

It's also better to create different environments for the different applications we are working on so that we don't mix up the variables from one application into another.

Variables provide a dynamic nature to the values we use in our application. We can set variables in different scopes, thus we not have to carry the values from the requests manually from place to place.

Postman supports the following variable scopes:

  • Global
  • Collection
  • Environment
  • Data
  • Local

The heirarchy of variavbles scopes is like: Data < Local < Environment < Collection < Global

Alt Text

We can set the environments by clicking on the "gear" (⚙️) icon in the top right corner.

Click on the gear button

We can set the environment variables while adding the environments.

Enter the values and click

We can easily switch between the environments created.

Once done, we can see the environments.

In order to use the variables declared, we simply have to enclose the variables names in double curly braces.

Alt Text

Alt Text

Scripts:

Scripts can be of two types:

  • Pre-request Script
  • Test Script

Pre-request Scripts:

Pre-request Scripts gives us the option to execute something right before sending a request. This can be anything such as setting a variable.

Say, we want to set the date variable to the current date in every request, we can do it by adding the following in the Pre-Request Scripts section

pm.environment.set("time", new Date());  
Enter fullscreen mode Exit fullscreen mode

Other Scripts (Tests):

The "Test" section serves a generic purpose of running test cases as well as using it to perform some other tasks such as setting up variables dynamically, chaining requests, etc., right after a request has finished running.

Alt Text

All this happens due to the Postman Sandbox.

Postman Sandbox is a JavaScript execution environment that lets us play with our requests and getting more out of it.

Postman also provides the console option where we can debug side-by-side the tests that we are writing.

We can make an entry into the console using the simple JavaScript console utilities such as

console.log(),
console.error(),
console.info()
Enter fullscreen mode Exit fullscreen mode

Now, let's say we have to save a parameter from the response into a variable. We can simply write a script for that.

Alt Text

The value for "token" will be set in the environment and we can use it later just like any other variable.

Alt Text

Chaining Requests:

Variables are not only useful in switching between the environments, but they also come really handy when we want to set the values from one request and use it into another. We can do this using the scripts we just learned about.

Let's see how we can do this:

Suppose we want to fetch the current weather of a particular place.

We are using two APIs to fulfill our purpose:

We send our location to LocationIQ and get the latitude-longitude pair. We then use these values to get the weather using the Dark Sky API.

Alt Text

The values have been set (we can see the preview on hovering)

Alt Text

We simply have to use the variables to get the values from the previous request into the current one.

Alt Text

Adding Tests:

We can run a sequence of tests on the completion of a request.

Postman provides us a lot of sample test scripts that we can simply add by clicking on it.

Alt Text

Here the first argument in the method test gives the name to that test, and the following argument is the function that executes the test.

Postman sandbox provides us a variety of options to test the desirability of the response. We can write them as:

pm.response.to.be.<condition>  
pm.response.to.not.be.<condition>  
pm.response.to.have.<condition>  
pm.response.to.not.have.<condition>  
Enter fullscreen mode Exit fullscreen mode

Say we have to check, in our weather example, that we get an OK response and the response always contains the current weather. We can do it as below:

Alt Text

And then we can see if our test cases have passed in the "Test Results" section

Alt Text

Running a Collection:

Postman provides us a feature called the Collection Runner. It helps us run all the APIs within a collection one after the other.

The Collection Runner then gives us the details of all the APIs that ran successfully with all their details.

Let's take a look.
I have a collection "Task Manager" which has a sub-folder "Users".

The Users folder contains the whole lifecycle of the user form creation to login to update to finally deletion.

Alt Text

Alt Text

On running the collection we get something like this:

Alt Text

All the green dots denote that the request was run successfully.

We can also click on a particular request to see its info:

Alt Text

Authorization:

Postman provides us a lot of options to authorize and authenticate our requests.

Alt Text

With respect to the option we choose, we are given further options corresponding to the option we have selected.

Say, if we choose OAuth 1.0, we will be required to fill out the details such as "Consumer Key", "Consumer Secret", "Access Token" and "Token Secret".

If we choose Bearer Token, then we simply have to input the JWT token which would be appended to the header of our requests, hence authenticating it.

Remember the example in the scripts section? No? Scroll up.

There we had set the token value from the response as an environment variable, we can simply use that value from the environment into our corresponding request, this sparing us of the hassle of copying the token everywhere to each one of our requests.

Alt Text

Wasn't it amazing.

Alt Text


That was all for this article. I have some more features in mind that I may write in future posts.

If you want to learn more about Postman features then refer to the following links:


Enjoy building APIs

Cheers 🍻

Top comments (2)

Collapse
 
ananyaneogi profile image
Ananya Neogi

This is great! Looking forward to the next post. 😁

Collapse
 
maydev profile image
Mayank Yadav

The response to this post has overwhelmed me. I'm so excited to write more. Thanks. 🤗