我有两个要求:邮递员中的A和B。我想先发送请求A,然后再发送请求B,而请求A仍在等待响应。手动执行此操作非常容易,因为请求A需要15秒才能完成。
但是无论如何,我可以自动执行此操作,因为我将对此案例进行很多测试。
我尝试在邮递员中使用转轮,但是它总是在发送请求B之前等待请求A完成。
此后,我在邮递员here中找到了有关发送异步请求的文档。
我编写了一个脚本,该脚本使用browser.storage.sync.get
发送请求B,并将该脚本放入请求A的预请求中。
pm.sendRequest
问题在于,即使我将其包装在let confirmRequest = {
url: url + "/confirm",
method: "POST",
body: {
"requestId": (new Date()).getTime(),
"statusCode": "0",
}
}
setTimeout(function() {
pm.sendRequest(confirmRequest, function (err, res) {
console.log(err ? err : res.json());
});
}, 1500);
函数中,请求A仍要等待 pre-request 首先完成。因此,最终请求B已在请求A之前发送。
这个问题有解决方案吗?
答案 0 :(得分:0)
I tried but could not achieve asynchronously process requests using Postman or Newman. I found it easier to write a nodeJS code using async-await-promise concepts. Here is the sample code:
适用于我的示例代码:
var httpRequest "your raw request body";
var headersOpt = {
"content-type": "application/json",
};
const promisifiedRequest = function(options) {
return new Promise((resolve,reject) => {
request(options, (error, response, body) => {
if (response) {
return resolve(response);
}
if (error) {
return reject(error);
}
});
});
};
var output;
async function MyRequest(httpRequest,j, fileName) {
var options = {
uri: "url",
method: "POST",
body: httpRequest,
json: true,
time: true,
headers: headersOpt
}
try {
console.log('request posted!');
let response = await promisifiedRequest(options);
console.log('response recieved!');
output = output + ',' +response.elapsedTime;
console.log(response.elapsedTime);
console.log(output);
//return response;
} catch (err) {
console.log(err);
}
finally
{
//this code is optional and it used to save the response time for each request.
try{
fileName = (j+1)+'_'+fileName;
fs.writeFile('/logs-async/scripts/output/'+fileName+'.csv', output, (err) => {
//throws an error, you could also catch it here
if (err) throw err;
});
}
catch (err){
console.log(err);
}
}
}