Save body results to a CSV or JSON file - using newman

Hey @hdabas,

Welcome to the community! :rocket:

Just jumping in here to see if I can help with getting the request name from the Newman run.

Newman has certain events that can be access with the .on() function - A full list can be found here:

A basic example of getting the name of the Request would be using something like this:

const newman = require('newman')

newman.run({
  collection: '<Postman Echo Collection>',
}).on('request', (err, args) => {
  console.log(args.item.name)
})

Here’s what that logs to the console, it’s just using the Postman Echo collection but this would be the same for your collections.

Saving each request to a file with the request name could be something like this:

const newman = require('newman'),
      fs = require('fs');

newman.run({
  collection: '<Postman Echo Collection>',
  folder: "Request Methods"
}).on('request', (err, args) => {
  fs.writeFile(`./results/${args.item.name}.json`, args.response.stream, (err) => {
    if (err) { 
        console.error(err); 
    }
  });
})

Again, I’ve used the Postman Echo Collection and reduced this down to just run the Request Methods folder. I also created a results folder to save the files into.

Screenshot 2020-06-17 at 10.08.37

Each of those files contains the JSON response body from that request.

4 Likes