Skip to content

Instantly share code, notes, and snippets.

@jonico
Created December 12, 2023 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonico/ed7ea756b5e052593e07e6412777776d to your computer and use it in GitHub Desktop.
Save jonico/ed7ea756b5e052593e07e6412777776d to your computer and use it in GitHub Desktop.
Duplicate Postman collection responses
var fs = require('fs'), // needed to read JSON file from disk
Collection = require('postman-collection').Collection,
Response = require('postman-collection').Response,
myCollection;
// Load a collection to memory from a JSON file on disk (say, sample-collection.json)
myCollection = new Collection(JSON.parse(fs.readFileSync('sample-collection.json').toString()));
// iterate through all requests in the collection
myCollection.forEachItem(function (item) {
// do something with the item
console.log(item.name);
// iterate through all responses of the item
item.responses.each(function (response) {
// do something with the response
console.log(response.name);
// duplicate the response and add it to the item, after changing the name to original name + copy
duplicateResponse = new Response(response.toJSON());
duplicateResponse.name = response.name + ' copy';
item.responses.add(duplicateResponse);
});
});
// save the collection back to disk
fs.writeFileSync('sample-collection-modified.json', JSON.stringify(myCollection.toJSON()));
// log items at root level of the collection
//console.log(myCollection.toJSON());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment