postman高级用法之pre-request

一、pre-request

1.简单用法
复制代码
//获取、设置、删除环境变量
pm.environment.get("variable_key");
pm.environment.set("variable_key", "variable_value");
pm.environment.unset("variable_key");
//获取设定删除全局变量
pm.globals.get("variable_key");
pm.globals.set("variable_key", "variable_value");
pm.globals.unset("variable_key");
//获取普通变量
pm.variables.get("variable_key");
//获取设置、删除集合变量
pm.collectionVariables.get("variable_key");
pm.collectionVariables.set("variable_key", "variable_value");
pm.collectionVariables.unset("variable_key");
​
//发送一个get请求
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});
复制代码

 

2.发送前置post请求,从中获取cookie,然后应用到第二个请求的header,常用于鉴权
复制代码
const loginRequest={
    url:"https://www.xxx.com/api/user/login",
    method:"POST",
     header: [
      "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
     "appVer: 4.4.0",
      ],
  body: {
    mode: 'raw',
    raw: 'loginName=user&loginPwd=pswd'
  }
};
pm.sendRequest(loginRequest,function(err,response){
    r = JSON.parse(JSON.stringify(response));
    for(var x of r.header){
        if(x.key=="Set-Cookie"){
            console.log(x.value);
            pm.collectionVariables.set("cookie",x.value);
        }
    }
  
});
复制代码

 

 

posted @   Mars.wang  阅读(1109)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示