当前位置:网站首页>Postman前置脚本-全局变量和环境变量
Postman前置脚本-全局变量和环境变量
2022-07-06 04:41:00 【炫酷的腿毛!】
全局变量和环境变量
全局变量和环境变量
全局变量
概念:在 Postman 全局生效的变量,全局唯一。
设置:
代码设置:
pm.globals.set(“全局变量名”,全局变量的值)// 示例: pm.globals.set("glb_age", 100)
获取:
代码获取:
var 接收值的变量 = pm.globals.get(“全局变量名”)// 示例: var ret = pm.globals.get("glb_age") // ret 的值为 100请求参数获取(postman界面获取):
{ {全局变量名}}// 示例: { { glb_age}}
环境变量
概念:在 特定环境(生产环境、开发环境、测试环境)下,生效的变量,在本环境内唯一。
设置:
代码设置:
pm.environment.set("环境变量名", 环境变量值)// 示例 pm.environment.set("env_age", 99)
获取:
代码获取:
var 接收值的变量 = pm.environment.get("环境变量名")// 示例 var ret = pm.environment.get("env_age") // ret 的值为 99请求参数获取(postman界面获取):
{ {环境变量名}}// 示例 { { env_age}}
Postman 请求前置脚本
- 在 send 按钮点击后,请求前置脚本代码,第一时间被执行。在 postman 内部实际 http请求之前。
假设,这样一种场景:
调某接口时,要输入 “时间戳”,如果输入的 “时间戳” 的绝对值,超过标准时间10分钟。 则不允许调用。
时间戳
- 时间戳:对应绝对时间,从 1970年1月1日00:00:00 到现在 所经历的秒数。
一天:86400秒
51年 6月(180) 9天 — 1,624,665,600 秒
案例
调用百度首页接口,传时间戳给服务器
实现步骤:
在 Pre-request Script 标签页中,添加代码。拿到时间戳,写入全局变量
// 拿到时间戳 var timestamp = new Date().getTime() // 将时间戳设置到 全局变量 pm.globals.set("glb_timestamp", timestamp)点击 Send 按钮,发送请求。请求发送前执行 上述代码。写入全局变量
查看写入的变量

- 在 请求参数(界面)中,使用全局变量。{ {全局变量名}}

- 在 postman的 控制台,查看 发送的 http请求

工作原理

边栏推荐
- newton interpolation
- 1008 circular right shift of array elements (20 points)
- NPM command -- install dependent packages -- Usage / explanation
- C. The Third Problem(找规律)
- Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
- Digital children < daily question> (Digital DP)
- ISP学习(2)
- Platformio create libopencm3 + FreeRTOS project
- Lombok principle and the pit of ⽤ @data and @builder at the same time
- 力扣(LeetCode)186. 翻转字符串里的单词 II(2022.07.05)
猜你喜欢

How does computer nail adjust sound
![[network] channel attention network and spatial attention network](/img/b5/5e746f0dd6badcf0714cae05fc6e82.jpg)
[network] channel attention network and spatial attention network

Vulnerability discovery - vulnerability probe type utilization and repair of web applications

A blog to achieve embedded entry

JVM garbage collector concept

coreldraw2022新版本新功能介绍cdr2022

Fedora/rehl installation semanage

Fuzzy -- basic application method of AFL

Programmers' position in the Internet industry | daily anecdotes

ORM aggregate query and native database operation
随机推荐
最高法院,离婚案件判决标准
newton interpolation
JVM garbage collector concept
coreldraw2022新版本新功能介绍cdr2022
. Net interprocess communication
SharedPreferences source code analysis
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
flink sql 能同时读多个topic吗。with里怎么写
Tengine kernel parameters
[FreeRTOS interrupt experiment]
canal同步mysql数据变化到kafka(centos部署)
[network] channel attention network and spatial attention network
ORM aggregate query and native database operation
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
Mixed development of QML and QWidget (preliminary exploration)
11. Intranet penetration and automatic refresh
Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
【Try to Hack】john哈希破解工具
ue5 小知识 FreezeRendering 查看视锥内渲染的物体