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?
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.
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?
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?
"
this works fine, still its a bit hacky 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
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.