Postman environment variables¶
Postman environment variables allow users to store and reuse values—such as authentication tokens, server addresses, or user IDs—across multiple API requests^[600-developer__tools__vscode__RestClient-Postman.md].
Data Persistence and Reuse¶
By defining environment variables, sensitive data or frequently changing parameters can be injected into requests dynamically.^[600-developer__tools__vscode__RestClient-Postman.md] This eliminates the need to hard-code values in every request, making collections easier to maintain and share^[600-developer__tools__vscode__RestClient-Postman.md].
Configuration in Requests¶
Variables are typically defined in a separate section or file and referenced using double curly braces (e.g., {{variableName}}) within the request URL, headers, or body^[600-developer__tools__vscode__RestClient-Postman.md].
For example, in a REST Client file, a base URL might be defined as @localhostBWS = http://localhost:8080/..., and then referenced in a request as POST {{domain}}/sessions^[600-developer__tools__vscode__RestClient-Postman.md].
Dynamic Variable Assignment¶
Postman can automatically extract data from a server's response and save it as an environment variable using scripts^[600-developer__tools__vscode__RestClient-Postman.md]. This is commonly used to handle session tokens or UUIDs returned after a login request.
The following JavaScript example parses a response body and sets the id and token variables for subsequent requests^[600-developer__tools__vscode__RestClient-Postman.md]:
// 将返回信息解析成对象
var responseData = JSON.parse(responseBody);
var id = responseData.id;
pm.environment.set("id", id);
var token = responseData.token;
pm.environment.set("token", token);
Related Concepts¶
- [[REST Client]]
- [[API Testing]]
- [[Dynamic Data Binding]]
Sources¶
^[600-developer__tools__vscode__RestClient-Postman.md]