当前位置:网站首页>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请求
工作原理
边栏推荐
- ISP学习(2)
- Certbot failed to update certificate solution
- Solve the compilation problem of "c2001: line breaks in constants"
- JVM garbage collector concept
- Fedora/rehl installation semanage
- Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
- 比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
- SQL注入漏洞(MSSQL注入)
- Ue5 small knowledge points to enable the setting of lumen
- 拉格朗日插值法
猜你喜欢
Dry goods collection | Vulkan game engine video tutorial
coreldraw2022新版本新功能介绍cdr2022
Mysql database storage engine
CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
RTP gb28181 document testing tool
11. Intranet penetration and automatic refresh
VNCTF2022 WriteUp
Digital children < daily question> (Digital DP)
麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
随机推荐
SQL injection vulnerability (MSSQL injection)
The implementation of the maize negotiable digital warehouse receipt standard will speed up the asset digitization process of the industry
RTP GB28181 文件测试工具
English Vocabulary - life scene memory method
Introduction to hashtable
Excellent PM must experience these three levels of transformation!
Lombok principle and the pit of ⽤ @data and @builder at the same time
SharedPreferences source code analysis
P2022 interesting numbers (binary & digit DP)
After learning classes and objects, I wrote a date class
Certbot failed to update certificate solution
Easyrecovery reliable and toll free data recovery computer software
BOM - location, history, pop-up box, timing
【HBZ分享】云数据库如何定位慢查询
Introduction of several RS485 isolated communication schemes
2328. 网格图中递增路径的数目(记忆化搜索)
IPv6 comprehensive experiment
Mysql database storage engine
Etcd database source code analysis -- etcdserver bootstrap initialization storage
[detailed steps of FreeRTOS shift value for the first time]