Save response in CSV file via newman

I have a Json collection for generate token(post) -> Create New Item (Post) ->Get(itemid)
Already running via newman with html report. the need is to save the body in CSV file for (Create New Item)

I need to create 50 new items and save all those newly-generated items in CSV file? Can some one tell me how to do capture two values itemGeneratedBy and
itemNumber from json Response and save it in csv?

@singhsivcan @danny-dainton Sorry i need for this you guys as expert.

What have you tired so far? Can you show us at what point it’s not working?

What is the response body? Are those 2 properties you mentioned the only thing in the response?

Hi Danny,
I have tried same csvpackage and executed my scripts. And the CSV gives me the whole response.

image

No 2 property does exists but in some path for body (which i need). Below is the sample output

Since i want this iteration to run for 20 times or may be more and need only two itemid and itemGeneratedby from response

@danny-dainton
1 . How can i use only the Body of one request for 20 iteration? That is for 2nd (Create Item)
2. Can i extract itemid and itemGeneratedby from responsebody of 2nd request and save it in csv report ?

It looks like using Newman as a library, rather than from the command line with a specific reporter is going to be the option here.

You can tap into the different events and extract the data you want from the response and feed this into a CSV file.

The node ‘fs’ module will allow you to write the data into a file.

https://nodejs.org/api/fs.html

Just a hacky example piece of code and is not a finished or polished solution:

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

newman.run({
  collection: '<Collection File>',
}).on('console', function (error, args) {
      fs.appendFileSync('./results.csv', args.messages, function (error) {
  })
})

Add this to your request:

let itemOne = "<location of value in the response>",
    itemTwo = "<location of value in the response>";

console.log(`\n${itemTwo},${itemOne}`)

This will save the console statements to a csv file, it has multiple limitations and you could do the same thing to get the dat via the request event and narrow it down to just a specific request but this was a quick hack :smiley:

2 Likes

newmanevent
Thanks for sharing this.

New to newman library, how to work with that if i may ask?

Not sure what you mean.

How do you create a node project and use Newman as a library?

1 Like

Likewise :slight_smile: I B’cally have jenkins Ci machine that run my newman collection, and need is to save the Itemid from of response into csv. hope this works ? and have installed npm C:\mymachine\myname\AppData\Roaming\npm

I’ve added some basic instructions for using Newman as a library here.

2 Likes

ahh yes, already have it like this
image
image

also have newman-reporter-csv package inside the node_modules

Thanks for developing & posting here it’s awesome and working, just trying to understand it in deep.

2 Likes

Hi @gpub1

I was looking for ways to extract this type of responses (in csv file) when running a collection. Do you mind if I ask how you do this?
I am new to postman and to API Testing world, I would greatly appreciate your help.

Thank you

@andrea-ree Welcome to Postman Community

Sure will help, since you are new kindly pls check other solutions here, this might help.

you try to print the needed results in console first.

Thanks