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.
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.
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!
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
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!
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.
Each of those files contains the JSON response body from that request.
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.
If you could provide an visual example of what you mean that would be great.
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);
}
});
}
});
I know this is old but this is great info here! I’m curious to know how you solved this problem?
I’m also facing this issue.
I interpreted the original post as asking how to display the JSON response as output from running newman
on the command-line. Try adding this “test” to your collection:
pm.test("JSON echo", function () {
var jsonData = pm.response.json();
console.log(jsonData);
});
The output of “tests” shows when you run newman
--so try adding a “test” to your collection, but instead of assert, do this:
console.log(pm.response.json());
Totally missed this…
Are you still having issues here?
You’re only getting the request body and response body here, the Headers wouldn’t be saved anywhere because there’s no reference to them.
They are going to different files because the file names are different.
You would need to use a different fs
module function for them to be written to the same file.
That one would overwrite the other one…I think. Not 100% sure.
Yes. I am still having issue with this. I want to write request and response for each Tests in one file.What function has to use for this?
I would first change the file name to be the same for each so it’s writing to the same place and see what that’s giving you.
I think you’re probably going to need to use this rather than writeStream
- https://nodejs.org/api/fs.html#fs_fs_appendfile_path_data_options_callback
Please let me know how did you achieve this ? i am also in need to write the output(Custom response i.e only Token value) in csv file.
Hi @dannydainton:
I have got a straightforward question to you.
I have a set of collections in postman. I am using the GET to acquire some of the results from Sonar Cloud. The results are generated in the json format. Is there a way where we can save that onto a csv format ?
Any inputs for this is much appreciated and I can learn.
Regards,
Rahul
Welcome nrkrahul,
use custom reporting with new man you can save the result in csv format