var url = '/api/pc/ca/manager/member/modify/applyInfo/page'; //1.dologin var bsmpUrl = pm.environment.get("server.url.bsmp"); var username = pm.environment.get("bsmp.login.pc.username"); var password = pm.environment.get("bsmp.login.pc.password"); var salt = pm.environment.get("bsmp.login.pc.salt"); var orgId = pm.environment.get("bsmp.login.pc.orgId"); var orgLevel = pm.environment.get("bsmp.login.pc.orgLevel"); let echoPostRequest = { url: 'http://' + bsmpUrl + '/api/pc/proxy/login', method: 'POST', header: [ "Content-Type: application/json; charset=UTF-8" ], body: { mode: 'raw', raw: '{\"username\":\"'+username+'\",\"password\":\"'+password+'\",\"salt\":\"'+salt+'\"}' } }; let echoGetRequest = { url: 'http://' + bsmpUrl + '/api/pc/proxy/complete' + '?' + 'orgId='+ orgId + '&' + 'orgLevel=' + orgLevel + '&' + 'time=' + '0.366126544927456', method: 'GET' }; pm.sendRequest(echoPostRequest, function (err, res) { console.log("loginRsp:" + res.json()); console.log("token:" + res.json().data) //2.doSign var token = res.json().data; var str = token + url pm.globals.set("temp", str); var sign = CryptoJS.MD5(str).toString(); pm.globals.set("token",token ); pm.globals.set("sign", sign); }); pm.sendRequest(echoGetRequest, function (err, res) { console.log("res:" + res.json()); }); 如何将此代码中的两个接口,按照顺序调用
时间: 2024-02-24 22:53:50 浏览: 177
msvcp90_http:90you.com/_verify_dfs_
可以使用回调函数或Promise对象来保证这两个接口按照顺序调用。
使用回调函数的方式,可以在第一个请求的回调函数中调用第二个请求。例如:
```
var url = '/api/pc/ca/manager/member/modify/applyInfo/page';
var bsmpUrl = pm.environment.get("server.url.bsmp");
var username = pm.environment.get("bsmp.login.pc.username");
var password = pm.environment.get("bsmp.login.pc.password");
var salt = pm.environment.get("bsmp.login.pc.salt");
var orgId = pm.environment.get("bsmp.login.pc.orgId");
var orgLevel = pm.environment.get("bsmp.login.pc.orgLevel");
function doLogin(callback) {
let echoPostRequest = {
url: 'http://' + bsmpUrl + '/api/pc/proxy/login',
method: 'POST',
header: [
"Content-Type: application/json; charset=UTF-8"
],
body: {
mode: 'raw',
raw: '{\"username\":\"'+username+'\",\"password\":\"'+password+'\",\"salt\":\"'+salt+'\"}'
}
};
pm.sendRequest(echoPostRequest, function (err, res) {
console.log("loginRsp:" + res.json());
console.log("token:" + res.json().data);
var token = res.json().data;
var str = token + url;
pm.globals.set("temp", str);
var sign = CryptoJS.MD5(str).toString();
pm.globals.set("token",token );
pm.globals.set("sign", sign);
callback();
});
}
function doSign() {
var token = pm.globals.get("token");
var sign = pm.globals.get("sign");
let echoGetRequest = {
url: 'http://' + bsmpUrl + '/api/pc/proxy/complete' + '?' + 'orgId='+ orgId + '&' + 'orgLevel=' + orgLevel + '&' + 'time=' + '0.366126544927456' + '&' + 'token=' + token + '&' + 'sign=' + sign,
method: 'GET'
};
pm.sendRequest(echoGetRequest, function (err, res) {
console.log("res:" + res.json());
});
}
doLogin(function() {
doSign();
});
```
使用Promise对象的方式,可以通过链式调用then方法来保证接口按照指定的顺序调用。例如:
```
var url = '/api/pc/ca/manager/member/modify/applyInfo/page';
var bsmpUrl = pm.environment.get("server.url.bsmp");
var username = pm.environment.get("bsmp.login.pc.username");
var password = pm.environment.get("bsmp.login.pc.password");
var salt = pm.environment.get("bsmp.login.pc.salt");
var orgId = pm.environment.get("bsmp.login.pc.orgId");
var orgLevel = pm.environment.get("bsmp.login.pc.orgLevel");
function doLogin() {
return new Promise(function(resolve, reject) {
let echoPostRequest = {
url: 'http://' + bsmpUrl + '/api/pc/proxy/login',
method: 'POST',
header: [
"Content-Type: application/json; charset=UTF-8"
],
body: {
mode: 'raw',
raw: '{\"username\":\"'+username+'\",\"password\":\"'+password+'\",\"salt\":\"'+salt+'\"}'
}
};
pm.sendRequest(echoPostRequest, function (err, res) {
console.log("loginRsp:" + res.json());
console.log("token:" + res.json().data);
var token = res.json().data;
var str = token + url;
pm.globals.set("temp", str);
var sign = CryptoJS.MD5(str).toString();
pm.globals.set("token",token );
pm.globals.set("sign", sign);
resolve();
});
});
}
function doSign() {
var token = pm.globals.get("token");
var sign = pm.globals.get("sign");
let echoGetRequest = {
url: 'http://' + bsmpUrl + '/api/pc/proxy/complete' + '?' + 'orgId='+ orgId + '&' + 'orgLevel=' + orgLevel + '&' + 'time=' + '0.366126544927456' + '&' + 'token=' + token + '&' + 'sign=' + sign,
method: 'GET'
};
pm.sendRequest(echoGetRequest, function (err, res) {
console.log("res:" + res.json());
});
}
doLogin()
.then(function() {
doSign();
});
```
阅读全文