Introduction:
Postman is one of the most popular API-development environments and needs no introduction to API developers. But strangely, numerous API developers often ignore many of its key features and make their work unnecessarily difficult.
As million of backend developers are relying on this wonderful tool, I have learnt something new from it, or I must say learnt late if some one knows it already.But I firmly believe that, there are people like me who are still figuring something on a daily basis, and want to learn something new even though it is late. So this article is for the developers who want to explore more about Postman tool.
key features
In this article, we’ll discuss the key features every API developer should be aware of.
Create an Account and Remain Logged In
This will preserve your history and collections.
Postman Collections And It’s Usage
A postman collection is nothing but a way to store your requests in a folder, so that you don’t want to run your requests with the same details again and again.
It also saves your time, for choosing out the requests for a particular reusable endpoint.
Workspaces
You can create multiple workspaces to isolate all collections, environments, etc.
There are three different kinds of Postman workspaces for your different needs: personal workspaces, team workspaces, and public workspaces.
Environment in Postman
Now this is some advanced stuff, but it is indeed an important part of Postman, which I believe you would want to know.
Environment is nothing but to make the user store some of variables and it’s value for reusing it in making a postman requests. Like, using same url again and again, or any value which is required every time, and you are sick of providing it again and again. For example, JWT token for fetching data for any JWT Required requests.
Understanding Pre-request Script and Tests Script
Pre-request Script
As the name implies, the pre-request script is executed before the request is made. One of the main
usages of the pre-request script is to make your request more dynamic.
For example, if a request requires the current date, we can use the moment library to get the current
date before passing it to the request body.
var moment = require("moment")
pm.environment.set("currentDate", moment().format("DD/MM/YYYY"))
Tests Script
Tests scripts are executed after the request is made. They can be used to perform assertions, set or
remove variables etc.
Chain Requests Using Variables
Suppose you want to send two requests sequentially:
Login: This will give you an authorization token
Update profile: You’ll use the above token in this
How did you pass the authorization token from the first request to the second?
Many developers would manually copy the token and paste it in the second request. But Postman provides a better way — you can store the token as an environment variable and then use it in subsequent requests. For example, to store the token in a variable, you can use some script like the following in the Tests tab of the first request:
if (responseCode.code === 200)
postman.setEnvironmentVariable("authHeader", postman.getResponseHeader("Token"));
Request Chaining is one of the easiest ways to increase your productivity. Suppose you have 2 APIs, and to execute the second API, you need to pass a token from the response of the first API. Instead of copying and pasting the values every time you run these APIs, you can use request chaining to automate this process for you.
Use Postman for API Documentation
Postman allows you to generate beautiful API documentation from your collections. Here’s an example.
So while Swagger seems to be more popular for documenting APIs, why not use Postman instead, at least internally? That’ll reduce your effort, assuming you’ll be using Postman collections anyway. Of course, there are frameworks like Springfox for generating API documentation from source code, but they don’t seem to be as powerful as Postman. In any case, why repeat yourself?
Generate Unique Values Using Dynamic Variables
Suppose you have a signup request, which expects a unique new email ID each time. So would you keep manually providing a new email ID every time when using the request?
Postman provides a better way — you can use a dymanic variable. Postman comes with three dynamic variables that you can use in your requests:
{{$guid}}: A v4 style guid
{{$timestamp}}: Current timestamp
{{$randomInt}}: A random integer 0-1000
So in the sign-up case, just use {{$guid}}@example.com as the unique email.
Monitors are a great way to stay up to date on the health and performance of your APIs. They run through your collections and give you the status of your tests, but they’re really only as good as your tests. By running the same monitor automatically in different environments, you can test against a multitude of scenarios throughout the day. This helps you flag issues early and achieve continuous and automated regression testing. The combination of monitors and environments also allows you to run monitors against different development environments by having an environment for staging and production.
Conclusion:
To sum up, Postman provides many clever ways to work with it efficiently. I touched on some of the basics in this article, but it also comes with many advanced features — e.g., collection- and folder-level variables and scripts, which you can use to make your job easy.