Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Postman REST Client Cleverly — Some Quick Tips

DZone's Guide to

Using Postman REST Client Cleverly — Some Quick Tips

Read this article in order to learn more about using Postman REST client and how to use environments as well as collections.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

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 issues 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: https://prod.example.com
  2. Test: http://test.example.com
  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 https://prod.example.com, http://test.example.com 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 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 dynamic 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}}@example.com 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) + "@example.com");

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 generating beautiful API documentation 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 provides many clever ways to work with it efficiently. 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 the free live book Spring Framework Recipes For Real World Application Development.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
api ,rest ,rest api ,postman ,postman tutorial ,postman collections ,software ,integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}