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?

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

Thanks!

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.

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?

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,