Postman is one of the most popular API development environments, which needs no introduction to API developers. But strangely, numerous API developers often ignore many of its key features and make their work unnecessarily difficult. In this post, we'll discuss such key features that every API developer should be aware of.

Before we begin, a useful tip: prefer the native version of Postman rather than the browser extension. Otherwise, you may face CORS related isssues sometimes.

1) Create an account and remain logged-in

This will preserve your history and collections.

2) Use collections

Save your requests in collections, so that they can be reused and shared. Create folders inside collections for better organization.

3) Use Environments

Postman allows to create and use Environments. For example, let's say you have an API running in three environments:

  1. Production:
  2. Test:
  3. Local: http://localhost:8080

So, you can create three Postman environments - say ExampleProd, ExampleTest and ExampleLocal. Then, when using Postman, you can set the current environment to one of these.

"But, what's the benefit?"

Postman allows you to store variables in environments, and use those in requests. For example, you can define a baseUrl variable in each of ExampleProd, ExampleTest and ExampleLocal environments, having values, and http://localhost:8080 respectively. Then, if you'll have a request's URL as {{baseUrl}}/users/5, when executing the request, Postman will replace {{baseUrl}} with its value in the current environment. This way, the same request can be reused in multiple environments, just by altering the current environment in Postman.

4) Chain requests using variables

Suppose you want to send two requests, sequentially:

  1. Login - this will give you an authorization token
  2. Update profile - you'll use the above token in this

How'd 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"));

The above will store the value of the Token response header in an authHeader variable. Then, as we discussed in (3) above - you can use {{authHeader}} in subsequent requests.

Request chaining is a powerful feature, which can be leveraged to run automated tests. For example, you can use it to smoke test your production deployments.

5) 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 between 0 and 1000

So, in the signup case, just use {{$guid}} as the unique email!

6) Use pre-request scripts to generate reusable unique variables

In the above example of generating a unique email id, there's a problem if you need to reuse the same id in a subsequent request. For example, what if you wanted to login after signup, using the same email id?

The solution is to use a pre-request script, instead of dynamic variables, to generate the email. For example, in the signup request, you can add the following script in the "Pre-request Script" tab:

pm.environment.set("registeredEmail", Math.floor(Math.random() * 10000000) + "");

This will generate a random email id and store it in a registeredEmail environment variable. You can then use {{registeredEmail}} in the same request as well as subsequent requests.

7) Use Postman for API documentation!

Postman allows to generate beautiful API documentations from your collections. Here is 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. Nonetheless, why repeat yourself?

To sum up, Postman provide many clever ways to work with it effeciently. We touched some basics in this post, 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.

NOTE: This article is derived from our live book Spring Framework Recipes For Real World Application Development.