Newman library in node.js script: access iterationData value for each iteration cycle

Hello,

I have a node.js script reading data from an inpout CSV file and making request to a 3rd party API. For each iteration of the input data I want to log to the console the values read but can not fine a way to do so? I have looked into the ‘args’ content for a number of events and can not find any populated with the data read from the input file?

thanks

Hey @guiom

Welcome to the community! :star:

The best way to see what’s happening is to share the script that you’re using, it’s far easier that trying to describe it :grin:

there is nothing to the script really:

the initial section is as follows:

newman
    .run({
        collection: options.collection,
        environment: options.environment,
        iterationData: options.data,
        reporters: "cli",
        verbose: true
    })

I then have a section dealing with the response data from the remote API:

.on('request', function(error, args) {
    if(args.response.json()) {
     write response body to file
    }

}

what I need is to be able to write to the output file the data read from the input CSV and I can not find a way to access the iteration data in any of the events.

the input data for example would be:

number
12345
23456
78901
etc.

on write the response body to the file I would for example what to prepend the ‘number’ value to the line entry

This is something that I would use to run the Collection and write the response to a file:

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

newman.run({
  collection: 'collection.json',
  environment: 'environment.json',
  iterationData: 'data.json'
}).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) }
        console.log(`File Created`)
    })
  }
})

Not sure I follow the part about the iteration data from the CSV as you have that separate to the collection now in the iterationData property.

You might need to explain that flow a little more.

thanks and yes that is fine to write the response data to file and essentially how it is done currently.

With regards to the iteration data, you say this is available in the iterationData property. How would you output a console entry with the data (from the iteration data at the current index) being used in the current request?

for sake of argument if my iterationData: ‘data.csv’ is as follows:

field1,field2
a,b
c,d
e,f
etc.

So effectively 3 iterations of data with 2 values each.

As you write the response stream to file. How would I get the field1 and field2 values that were used in the corresponding request?

I tried to answer that here:

The only thing that you would need to change is the way you get the values from the data field during the different iterations:

let fieldOne = pm.iterationData.get('field1'),
    fieldTwo = pm.iterationData.get('field2');

console.log(`\n${fieldOne},${fieldTwo}`)
1 Like

ah great thanks. I seem to be getting “ReferenceError: pm is not defined” and presumably need to reference it elsewhere in the script for this to work. Obviously within say a Postman pre-request script and environment it is defined, but how to do this within a node.js script?
"

Hmm…that’s odd. Which version of Postman are you using?

It just needs to be in the Collection in the places that I mentioned - As in, the code above needs to go in the Pre-request Script.

You don’t need to do anything in the node script apart from adding the .on('console') part from the linked thread.

1 Like

ah ok fine I get it and many thanks.

this works fine, still its a bit hacky :wink: I would have thought that accessing the iteration data directly from the events in a node.js script would be quite a common requirements. In the same way that saving the response body to file directly from Postman is quite an obvious requirement.

Many thanks for your help, prompt response and so on. This is much appreciated

If you’re using the datafile with Newman, you would have it stored somewhere in the first place.

newman.run({
  iterationData: 'data.json'
})

It’s hacky because it feels like, a round the houses way of getting the data via the Collection run, that you already have available before the run. :smiley:

There is an option to Send and Download the response body to a file when sending individual requests but it’s not part of the Collection Runner.

All feature requests can be added here though:

ok let me explain further and maybe it will make sense.

the input file is a number, say a telephone number. this number is used to query a data source which returns a response body with some data related to the number. It does not however echo back the number itself. So to write the number to file together with the response body I need to access to iterationData as the request is made. It works via preRequest scripts and the on console event as we discussed above.

The data is indeed in the options for the Newman run

newman.run({
       iterationData: 'data.json'
})

I dont know if this iterationData can be accessed in the events which is why I have to go via the console at the moment. There is possibly a better way but I havent found it so far.