Mukil Deepthi
Mukil Deepthi

Reputation: 6482

Postman how to export api response from collection runner with iteration to a file using node script

I am completely new to writing node scripts in Postman.

My requirement: I have an api to get user details. I want to iterate for n number of users. I created a Runner collection and it executes. But i want to write each request response to a file.

Can anyone help me how to do this?

I watched some youtube video https://www.youtube.com/watch?v=cCRmry10874 for this. But my case is i have runner collection with data file.

When i exported the collection, i dont get the different values from data file.

const newman = require('newman');

newman.run({
    collection: require('./collection.json'),
    reporters: 'cli'
}, (err) => {
    if(err) { throw err; }
    console.log('collection run complete');
});

const fs = require('fs');
fs.writeFile('response.txt', 'Some text', (error) => {
    if(error) {
        console.error(error);
    }
})

Thanks

Upvotes: -1

Views: 548

Answers (2)

Reinhart Buit
Reinhart Buit

Reputation: 3

If you are not limited to using a textfile, I would suggest using htmlextra. It provides an HTML webpage with your runs and response body.

Upvotes: 0

Ah Hu
Ah Hu

Reputation: 134

Can you try it?

newman.run(
  {
    collection: require("./collection.json"),
    reporters: "cli",
    iterationData: "./data.json",
  },
  (err, summary) => {
    if (err) {
      throw err;
    }
    const results = summary.run.executions;
    results.forEach((result) => {
      fs.appendFileSync("response.txt", result.response.text());
    });
  }
);

Upvotes: 0

Related Questions