Backend Development 11 min read

Master Postman: Hidden Features for Efficient API Testing and Automation

This article reveals powerful yet often overlooked Postman features—including environment variables, global and collection scopes, pre‑request scripts, test scripts, and token automation—demonstrating how to streamline API debugging across multiple environments, automate authentication, and manage variable lifecycles for more efficient development workflows.

macrozheng
macrozheng
macrozheng
Master Postman: Hidden Features for Efficient API Testing and Automation

Postman is a widely used API debugging tool, but many users only use its basic request‑send feature. This guide introduces several practical, often ignored functions that can greatly improve your workflow.

Environment Variables

During a project's lifecycle you may have development, testing, pre‑release, and production environments. Environment variables let you manage host addresses and ports for each environment.

In the left sidebar click

Environments

. By default a global environment

Globals

exists for common variables. Add

host

and

port

values there.

Reference a variable in a request using double curly braces:

{{variable}}

.

You can also create custom environments. In the example we create

local

and

test

environments, allowing quick switching without manually editing request URLs.

If a variable name exists in both the active environment and the global environment, the active environment's value takes precedence and overrides the global one.

Global variables have a broader scope and remain available even when you switch to a custom environment, while custom environments are isolated from each other.

To modify variables programmatically, Postman provides

Pre-request Script

and

Tests

modules where you can run JavaScript.

Pre-request Script

Run js script

The

Pre-request Script

runs before a request is sent, allowing you to execute JavaScript code. The example below converts a timestamp parameter to a date and prints it.

<code>@GetMapping("test1")
public void time(@RequestParam("time") String time){
    Date date = new Date(Long.parseLong(time));
    System.out.println(date);
}
</code>

In a pre‑request script you can obtain the current time and store it in a collection variable for the request:

Sending the request prints the call time in the console:

<code>Tue Aug 01 14:14:29 CST 2021
</code>

Send GET request

The pre‑request script can also call another API before the main request using

sendRequest

with a GET method.

After the GET request finishes, the actual target request is executed. You can also configure POST, headers, or JSON payloads as needed.

Send POST request

A common scenario is obtaining a token from a login API before calling a protected endpoint. The pre‑request script can fetch the token and store it in a collection variable.

Store the token:

<code>pm.collectionVariables.set("TOKEN", response.json().data.token);
</code>

Then reference

{{TOKEN}}

in the Authorization header of subsequent requests.

If the API expects JSON payloads, you can set the body like this:

<code>body: {
  mode: 'raw',
  raw: JSON.stringify({ key: 'value' })
}
</code>

Custom headers can be added in the request definition:

<code>const loginRequest = {
  url: '...',
  header: [
    'Key1: Value1',
    'Key2: Value2'
  ],
  ...
};
</code>

Tests

The

Tests

script runs after a request completes. Using it, you can cache a JWT token obtained from a login call, avoiding repeated token requests.

Subsequent requests can simply use

{{TOKEN}}

, which will be automatically replaced with the cached value, ensuring the token stays up‑to‑date without manual copying.

Postman executes all pre‑request scripts in the order: collection → folder → request, then runs the actual request, followed by all test scripts in the same hierarchical order. Understanding this execution flow is essential to avoid missing or incorrectly updating variable values.

Automationbackend developmentAPI testingPostmanenvironment variablespre-request script
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.