当前位置:网站首页>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请求

工作原理

边栏推荐
- 程序员在互联网行业的地位 | 每日趣闻
- IPv6 comprehensive experiment
- Introduction of several RS485 isolated communication schemes
- P2022 有趣的数(二分&数位dp)
- How does vs change the project type?
- Mixed development of QML and QWidget (preliminary exploration)
- Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
- Excellent PM must experience these three levels of transformation!
- 8. Static file
- Solve the compilation problem of "c2001: line breaks in constants"
猜你喜欢

The most detailed and comprehensive update content and all functions of guitar pro 8.0

ue5 小知识点 开启lumen的设置

How to realize automatic playback of H5 video

SQL注入漏洞(MSSQL注入)

Dry goods collection | Vulkan game engine video tutorial

Use sentinel to interface locally

Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
![[detailed steps of FreeRTOS shift value for the first time]](/img/73/a469eb2465bb2c5acaa4d018d3edd3.jpg)
[detailed steps of FreeRTOS shift value for the first time]

Programmers' position in the Internet industry | daily anecdotes

程序员在互联网行业的地位 | 每日趣闻
随机推荐
也算是学习中的小总结
Redis has four methods for checking big keys, which are necessary for optimization
tengine 内核参数
English Vocabulary - life scene memory method
[try to hack] John hash cracking tool
动态规划(树形dp)
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
Platformio create libopencm3 + FreeRTOS project
牛顿插值法
How does vs change the project type?
关于es8316的音频爆破音的解决
团队协作出了问题,项目经理怎么办?
Recommendation | recommendation of 9 psychotherapy books
What should the project manager do if there is something wrong with team collaboration?
最高法院,离婚案件判决标准
flink sql 能同时读多个topic吗。with里怎么写
npm命令--安装依赖包--用法/详解
Visio draw fan
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
[Zhao Yuqiang] deploy kubernetes cluster with binary package