Postman预请求脚本执行顺序不符,依赖Token未正确获取求助
Postman预请求脚本异步请求顺序问题
我是Postman新手,尝试编写预请求脚本完成认证与授权的繁琐操作,该流程需要两个请求,脚本代码如下:
console.log("START"); var authenticationToken; // Identity token var authenticationTokenRequest = { url: 'xxxx', method: 'POST', timeout: 0, header: { "Content-Type": "application/x-www-form-urlencoded", "Authorization": "Basic xxxxx=" }, body: { mode: 'urlencoded', urlencoded: [ {key: "grant_type", value: "password"}, {key:"username", value: "xxxxx"}, {key:"password", value: "xxxx"}, ]} }; pm.sendRequest(authenticationTokenRequest, function (err, res) { console.log("01 send first request body"); var responseJson = res.json(); console.log(responseJson); pm.environment.set('ACCESS_TOKEN', responseJson['access_token']); authenticationToken = responseJson['access_token']; }); console.log("Authentication token local var: " + authenticationToken); console.log("Authorization token env var: " + pm.environment.get('ACCESS_TOKEN')); var authorizationTokenRequest = { url: "xxxx", method: "POST", header: { "Content-Type": "application/json", "Authorization": "Bearer " + authenticationToken, "Accept": "application/xxx+json" }, body:{ tenantId: "xxx", deviceId: "xxx" } } pm.sendRequest(authorizationTokenRequest, function (err, res) { console.log("02 second request call"); var responseJson = res.json(); pm.environment.set('ACCESS_TOKEN', responseJson['access_token']); });
运行脚本后,控制台显示本地变量authenticationToken为undefined,且第二个请求的日志先于第一个请求出现,第二个请求依赖第一个请求返回的值,请问哪里操作有误?
问题原因
核心问题是异步执行逻辑:pm.sendRequest是异步方法,发起请求后会直接继续执行后续代码,不会等待请求返回。你的代码中,第一个请求的回调函数还没执行(还没给authenticationToken赋值),后续的打印日志、创建并发送第二个请求的代码就已经执行完毕,因此出现:
- 打印
authenticationToken时变量未赋值,显示undefined - 第二个请求先于第一个请求完成日志输出
- 第二个请求的
Authorization头携带的token是undefined,请求无效
修正后的代码
将第二个请求的逻辑完全移入第一个请求的回调函数内部,确保第一个请求完成并拿到token后,再执行第二个请求:
console.log("START"); // Identity token var authenticationTokenRequest = { url: 'xxxx', method: 'POST', timeout: 0, header: { "Content-Type": "application/x-www-form-urlencoded", "Authorization": "Basic xxxxx=" }, body: { mode: 'urlencoded', urlencoded: [ {key: "grant_type", value: "password"}, {key:"username", value: "xxxxx"}, {key:"password", value: "xxxx"}, ]} }; pm.sendRequest(authenticationTokenRequest, function (err, res) { console.log("01 send first request body"); var responseJson = res.json(); console.log(responseJson); pm.environment.set('ACCESS_TOKEN', responseJson['access_token']); var authenticationToken = responseJson['access_token']; // 第一个请求完成后再执行后续逻辑 console.log("Authentication token local var: " + authenticationToken); console.log("Authorization token env var: " + pm.environment.get('ACCESS_TOKEN')); var authorizationTokenRequest = { url: "xxxx", method: "POST", header: { "Content-Type": "application/json", "Authorization": "Bearer " + authenticationToken, "Accept": "application/xxx+json" }, body:{ tenantId: "xxx", deviceId: "xxx" } } pm.sendRequest(authorizationTokenRequest, function (err, res) { console.log("02 second request call"); var responseJson = res.json(); pm.environment.set('ACCESS_TOKEN', responseJson['access_token']); }); });
修正说明
- 把第二个请求的创建、发送逻辑全部放入第一个请求的回调函数中,保证执行顺序:第一个请求返回token → 第二个请求使用该token发起请求
- 将
authenticationToken变量移至回调内部声明,避免全局变量的异步赋值问题 - 打印日志的语句也移入回调,确保打印时变量已被正确赋值
内容的提问来源于stack exchange,提问作者user2919960
相关产品推荐
相关产品推荐

