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

工作原理

边栏推荐
- 优秀PM必须经历这3层蜕变!
- Selection sort
- JVM garbage collector concept
- SQL注入漏洞(MSSQL注入)
- [Zhao Yuqiang] deploy kubernetes cluster with binary package
- Visio draw fan
- Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
- Microservice resource address
- [network] channel attention network and spatial attention network
- web工程导入了mysql驱动jar包却无法加载到驱动的问题
猜你喜欢

二叉树基本知识和例题

DMA use of stm32
![[FreeRTOS interrupt experiment]](/img/8f/54422d346bb54d23fab824be2f17a3.jpg)
[FreeRTOS interrupt experiment]

Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps

Is the mode of education together - on campus + off campus reliable

CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)

Implementation of knowledge consolidation source code 1: epoll implementation of TCP server

Bill Gates posted his 18-year-old resume and expected an annual salary of $12000 48 years ago

Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code

Recommendation | recommendation of 9 psychotherapy books
随机推荐
View workflow
Programmers' position in the Internet industry | daily anecdotes
Mixed development of QML and QWidget (preliminary exploration)
【Try to Hack】john哈希破解工具
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
flink sql 能同时读多个topic吗。with里怎么写
ue5 小知识 FreezeRendering 查看视锥内渲染的物体
Digital children < daily question> (Digital DP)
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
RTP GB28181 文件测试工具
C. The third problem
CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
Tengine kernel parameters
【HBZ分享】云数据库如何定位慢查询
力扣(LeetCode)186. 翻转字符串里的单词 II(2022.07.05)
Supreme Court, judgment standard of divorce cases
动态规划(树形dp)
ETCD数据库源码分析——etcdserver bootstrap初始化存储
Use sentinel to interface locally
Crawler notes: improve data collection efficiency! Use of proxy pool and thread pool