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

Its possible to save the body results to a CSV or JSON file, using newman?

Capture

I want to save the results, just like the image.

Thanks!

1 Like

Hi @portescheller,

Newman is in essence a CLI collection runner, so if you are referring to downloading the results of a collection run onto a json file you can run the following commands on the CLI:

newman run mycollection.json --reporters cli,json --reporter-json-export outputfile.json

To export the results onto a CSV file, you can download the reporter from the following link and enter the appropriate command on the CLI:

But based on the screenshot, I am pretty sure you are referring to capturing the response body of a particular request and exporting it into a JSON file, for that particular use case, you can click on the arrow next to the “Send” button as shown in the attached image and then click on “Send and Download” to download the JSON response body as a JSON file onto your system.

1 Like

Yes, you are correct, I need “response body of a particular request and exporting it into a JSON file” but I need this automatic in newman… its possible?

I’m running the command “newman run Get_MRS_Incidents.json --reporters cli,json --reporter-json-export outputfile.json”

But file generated, “outputfile.json” does not contain query result

Capture1

Any other idea?

Regards and thanks for your help!

Hi @portescheller,

The short answer would be that you cannot run a single request on Newman and export the results to a JSON file as Newman runs collections and not requests.
One workaround would be to create a collection, add the single request you require under that collection and run the Newman command for the exported collection. Although this is not a very effective use of Newmans capabilities.

so, is there another way to automate this task without the newman?

1 Like

Hi @portescheller,

You cannot automate with task without Newman / or you need to write your own script to do that outside of Postman.

Take a look at this article on writing files to disk:

Let me know if it helps you.

1 Like

Hi, vdespa

Sorry, i’m totally newbie on Postman/Newman

Using the script:

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

newman.run({
    collection: require('./GET_MRS.json'),
    reporters: 'cli'
}).on('beforeRequest', function (error, args) {
    if (error) {
        console.error(error);
    } else {
        fs.writeFile('request.txt', args.request.body.raw, function (error) {
            if (error) { 
                console.error(error); 
            }
        });    
    }
}).on('request', function (error, args) {
    if (error) {
        console.error(error);
    }
    else {
        fs.writeFile('response.json', args.response.stream, function (error) {
            if (error) { 
                console.error(error); 
            }
        });        
    }
});

The response is:

and no file is saved with the result.

Thanks for your help,

I am having the same issue with the files not being saved. Is there any response for that question?

This code seems to work fine for me and the files are created with the data. It would have created those files in the same directory as you ran the script.

You could add this to the writeFile function to log the file path:

console.log(`File Created in this dir: ${process.cwd()}`);

There also could be all sorts of environmental things happening here with the version of node, the version of Newman, the installation method of the npm package, the package.json file, the permissions to write files into that dir, etc.

@portescheller @jilllancaster

I think the root cause for this behaviour is trying to log a GET request which has no body.

You may want to surround the part with args.request.body.raw by first checking if args.request.body is defined.

Let me know if this helps.

1 Like

vdespa - Your suggestion was spot on. That request had no “body”. I changed that to args.request.url.toString() and all works perfectly. Thank you so much. As a very new newbie to all this…your videos have been invaluable. So I thank you for those as well!

2 Likes

Hi @vdespa,

Your article was very much helpful in writing files to disk.

I want to write requests and responses of each iteration data request.

Could you please help me with that?

Please have a look at my code:

const newman = require('newman'),
      fs = require('fs');
	  
const fileName = Math.random().toString(36).substring(7) + '-Automation_Family_request.json';
const fileName_1 = Math.random().toString(36).substring(7) + '-Automation_Family_response.json';

newman.run({
    collection: require('./Automation_Family.postman_collection.json'),
	environment: require('./urlogi.postman_environment.json'),
	globals: require('./Globalenv.postman_globals.json'),
	iterationData: require('./Family.json'),
	iterationCount: 192,
	insecure: true,
    reporters: 'cli'
}).on('beforeRequest', function (error, args) {
    if (error) {
        console.error(error);
    } else {
		fs.writeFile(fileName, args.request.body.raw, function (error) {
            if (error) { 
                console.error(error); 
            }
        });    
    }
}).on('request', function (error, args) {
    if (error) {
        console.error(error);
    }
    else {
		fs.writeFile(fileName_1, args.response.stream, function (error) {
            if (error) { 
                console.error(error); 
            }
        });        
    }
});

Thanks,
Bharath

So what happen when you run the script posted? What seems to be the problem? @Bharath_Gaddam

Hi @vdespa

When I run the script it overwrites the previously saved request and response. By the end of the collection run, it saves the last iteration request and response…

But, I would like to save each and every iteration request and response instead of only saving the last iteration run request and response.

Thanks

Hi @vdespa… Thanks for the Help.

I figured out by exploring the File systems.

https://www.brainbell.com/javascript/fs-open-read-write-file.html
&
https://nodejs.org/api/fs.html#fs_file_system

Hi @vdespa @aamir.ahmed . Now let’s stretch a little bit more…

Suppose in a collection i have 5 requests & we have saved the response in external disk of all these 5 request. But the file name in which response saved should be equal to request name So, how can we do that ?
Is there any replica of postman function - console.log(request.name) ? functionality in 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.

5 Likes


Thanks @danny-dainton, it worked !

I was doing a little brainstorm & trying not to hardcode the requestName in postman UI (In attached screenshot request Name is login) & trying to pass the requestName from my external csv file.

Challenges :
How can i assign the value present in csv file to requestName in postman script section OR via newman ? Is this possible via postman for requestName ?

I don’t really know what you’re trying to do, you may need to expand of the actual problem you’re trying to solve.

  • What does your CSV file look like?
  • What do you mean by assign the value present?
  • Do you want that value in the CSV file to change the Request Name in the UI?

If you could provide an visual example of what you mean that would be great. :smiley:

Hi Danny,

Is there any way I can get request (Body and Header details) and response in one file?

I used below , but I am getting request and response in different file also getting only request body details not header details.

Could you please assist me on this.Thanks

.on('beforeRequest', function (error, args) {
    if (error) {
        console.error(error);
    } else {
        fs.writeFile(`./results/${args.item.name}_request.json`, args.request.body.raw, (err) => {
            if (error) { 
                console.error(error); 
            }
        });    
    }
}).on('request', function (error, args) {
    if (error) {
        console.error(error);
    }
    else {
        fs.writeFile(`./results/${args.item.name}_response.json`, args.response.stream, (err) => {
            if (error) { 
                console.error(error); 
            }
        });        
    }
});