How to write to local file system using a postman collection java script

  1. Ensure that you have node and npm installed.
  2. Install newman with: npm install newman.
  3. Create a file with the following contents:

var fs = require('fs'),
    newman = require('newman'),

    results = [];

newman.run({
    reporters: 'cli',
    collection: '/path/to/collection.json',
    environment: '/path/to/environment.json' // This is not necessary
})
.on('request', function (err, args) {
    if (!err) {
        // here, args.response represents the entire response object
        var rawBody = args.response.stream, // this is a buffer
            body = rawBody.toString(); // stringified JSON

        results.push(JSON.parse(body)); // this is just to aggregate all responses into one object
    }
})
// a second argument is also passed to this handler, if more details are needed.
.on('done', function (err, summary) {
    // write the details to any file of your choice. The format may vary depending on your use case
    fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
});

Here is another example:


var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // Body parser use JSON data

app.post('/launches', function(req, res) {

    var outputFilename = './spaceReport.json'; // path of the file to output

    fs.writeFileSync(outputFilename, JSON.stringify(JSON.parse(req.body.payload), null, 4)); // write to the file system

    res.send('Saved to ' + outputFilename);

});

var port = 3000;
app.listen(port);
console.log('Express started on port %d ...', port);

4 thoughts on “How to write to local file system using a postman collection java script

  1. Paul says:

    Hi,

    I started working with Postman from couple of days and I have the below issue where I currently struck.

    I have a GET request XML with parameters (no request body) which I have parameterrized and able to send the request with multiple data sets using csv file (using collection runner). But I need to save the response of each and every request, but I am not sure how to do it using a script. I need to save response in XML format to local drive. Can you please help me?

  2. Ourhints says:

    Sorry for late response. This can be best accomplished within Postman’s CLI companion, Newman. You would then write a script to generate your custom reports via the following steps:

    1. Ensure that you have node and npm installed.
    2. Install newman with: npm install newman.
    Create a file with the following contents:

    var fs = require('fs'),
        newman = require('newman'),
    
        results = [];
    
    newman.run({
        reporters: 'cli',
        collection: '/path/to/collection.json',
        environment: '/path/to/environment.json' // This is not necessary
    })
    .on('request', function (err, args) {
        if (!err) {
            // here, args.response represents the entire response object
            var rawBody = args.response.stream, // this is a buffer
                body = rawBody.toString(); // stringified JSON
    
            results.push(JSON.parse(body)); // this is just to aggregate all responses into one object
        }
    })
    // a second argument is also passed to this handler, if more details are needed.
    .on('done', function (err, summary) {
        // write the details to any file of your choice. The format may vary depending on your use case
        fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
    });
    

    Run the file with:
    node script.js # Here, script.js is the file created in the previous step

  3. Brooke Niten says:

    Wonderful, what a weblog it is! This weblog provides valuable information to us, keep it up.|

  4. Alinah says:

    Is anyone here in a position to recommend Triangle bikinis? Thanks xox

Leave a Reply to Paul Cancel reply

Your email address will not be published. Required fields are marked *