在 Ubuntu 上使用 Postman 调试脚本(Pre-request Script / Tests 脚本),可以从以下几个方面入手:
Postman 主要有两类脚本:
语言都是 JavaScript。
console.log()在脚本中写:
console.log("请求参数:", pm.request.url.toString());
console.log("环境变量:", pm.environment.get("token"));
查看日志:
Ctrl + Alt + CPostman Console 可以看:
打开方式:
View → Show Postman ConsoleCtrl + Alt + Cconsole.log(pm.variables.toObject());
console.log(pm.environment.toObject());
console.log(pm.globals.toObject());
try {
let data = JSON.parse(pm.response.text());
console.log(data);
} catch (e) {
console.error("解析失败:", e);
}
pm.test("状态码是200", function () {
console.log("实际状态码:", pm.response.code);
pm.response.to.have.status(200);
});
console.log("即将发送请求");
pm.environment.set("timestamp", Date.now());
.deb 安装:wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gz
sudo tar -xzf postman.tar.gz -C /opt
检查:
pm.execution 查看执行信息newman run collection.json
✅ Ubuntu 上调试 Postman 脚本的核心就是:
写
console.log()+ 打开 Postman Console
如果你有具体脚本或报错信息,可以直接贴出来,我可以帮你逐行调试。