Taking hint from here - http://blog.getpostman.com/2017/09/01/write-to-your-local-file-system-using-a-postman-collection/, below is a nodeJS server I wrote which will capture the requests and responses and print them one by one along with request name(which you have set in Postman) and URL.
var fs = require('fs'),
newman = require('newman'),
allRequest =[],
allResponse = [],
allName = [],
requestUrl = "",
allRequestUrl = [];
newman.run({
collection: '//your_collection_name.json',
iterationData: 'your_iteration_file',
iterationCount : 3
})
.on('request', function (err, args) {
if (!err) {
//console.log(args); // --> args contain ALL the data newman provides to this script.
var requestBody = args.request.body,
request = requestBody.toString();
allRequest.push(JSON.parse(request));
var responseBody = args.response.stream,
response = responseBody.toString();
allResponse.push(JSON.parse(response));
var nameBody = args.item.name;
allName.push(nameBody);
var protocol = args.request.url.protocol;
var host = args.request.url.host;
var path = args.request.url.path;
requestUrl+=protocol+"://";
for(var j = 0;j<host.length;j++)
{
requestUrl+= host[j];
if(j!=host.length-1)
{
requestUrl+=".";
}
}
requestUrl+='/';
for (var k =0;k<path.length;k++)
{
requestUrl+= path[k];
if(k!=path.length-1)
{
requestUrl+= "/";
}
}
allRequestUrl.push(requestUrl);
}
})
.on('done', function (err, summary) {
fs.writeFile('test.html',"");
//modify html output here.
for(var i =0;i<allRequestUrl.length;i++)
{
fs.appendFileSync('test.html', "<br><h>Name: </h>");
fs.appendFileSync('test.html',allName[i]);
fs.appendFileSync('test.html', "<br><h>URL: </h>");
fs.appendFileSync('test.html',"\"" + allRequestUrl[i] + "\"");
fs.appendFileSync('test.html', "<br><h>Request</h><br>");
fs.appendFileSync('test.html',JSON.stringify(allRequest[i],null,4));
fs.appendFileSync('test.html', "<br><h>Response</h><br>");
fs.appendFileSync('test.html',JSON.stringify(allResponse[i],null,5));
//fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
}
});
To run the above code, you need to install newman which is Postman's CLI.
First of all install node and npm in your computer, then go to your directory and install newman via -
npm install newman
Then copy paste the above code in a js file 'filename.js' and run it by below command -
node filename.js
The output containing the information you require will be saved in a file named "test.html" in the same directory.