DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Using Postman REST Client Cleverly — Some Quick Tips

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.

Sanjay Patel user avatar by
Sanjay Patel
CORE ·
May. 24, 18 · Tutorial
Like (10)
Save
Tweet
Share
13.92K Views

Join the DZone community and get the full member experience.

Join For Free

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.

REST Web Protocols Requests API

Published at DZone with permission of Sanjay Patel. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Discovery With Eureka
  • What Was the Question Again, ChatGPT?
  • Mr. Over, the Engineer [Comic]
  • How To Create and Edit Excel XLSX Documents in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: