New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] possibility to programatically write to a file #3033

Closed
mpluta-airhelp opened this Issue May 16, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@mpluta-airhelp

mpluta-airhelp commented May 16, 2017

Recently I was challenged by a task to copy certain objects from prod database to staging. I managed to finish it using API calls. At the end, I had to inspect the response of the last request to collect ID of the newly created object. It worked fine for 5-10 object but when I am dealing with thousands of objects to copy I would love to create a file on my disk and make Postman append IDs to this file (and possibly other data along the way). It could be also a great way to create a file which I could then use in another collection.
Is it possible currently in Postman to have this reporting functionality?

@kunagpal

This comment has been minimized.

Show comment
Hide comment
@kunagpal

kunagpal May 16, 2017

Member

@mpluta-airhelp This is best accomplished within Postman's CLI companion, Newman. You would then write a script to generate your custom reports via the following steps:

  1. Ensure that you have node and npm installed.
  2. Install newman with: npm install newman.
  3. Create a file with the following contents:
var fs = require('fs'),
    newman = require('newman'),

    results = [];

newman.run({
    reporters: 'cli',
    collection: '/path/to/collection.json',
    environment: '/path/to/environment.json' // This is not necessary
})
.on('request', function (err, args) {
    if (!err) {
        // here, args.response represents the entire response object
        var rawBody = args.response.stream, // this is a buffer
            body = rawBody.toString(); // stringified JSON

        results.push(JSON.parse(body)); // this is just to aggregate all responses into one object
    }
})
// a second argument is also passed to this handler, if more details are needed.
.on('done', function (err, summary) {
    // write the details to any file of your choice. The format may vary depending on your use case
    fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
});
  1. Run the file with:
node script.js # Here, script.js is the file created in the previous step

PS: If this is a recurring use case, then you could also turn the above script into a custom reporter (https://github.com/postmanlabs/newman#creating-and-using-custom-reporters)

Member

kunagpal commented May 16, 2017

@mpluta-airhelp This is best accomplished within Postman's CLI companion, Newman. You would then write a script to generate your custom reports via the following steps:

  1. Ensure that you have node and npm installed.
  2. Install newman with: npm install newman.
  3. Create a file with the following contents:
var fs = require('fs'),
    newman = require('newman'),

    results = [];

newman.run({
    reporters: 'cli',
    collection: '/path/to/collection.json',
    environment: '/path/to/environment.json' // This is not necessary
})
.on('request', function (err, args) {
    if (!err) {
        // here, args.response represents the entire response object
        var rawBody = args.response.stream, // this is a buffer
            body = rawBody.toString(); // stringified JSON

        results.push(JSON.parse(body)); // this is just to aggregate all responses into one object
    }
})
// a second argument is also passed to this handler, if more details are needed.
.on('done', function (err, summary) {
    // write the details to any file of your choice. The format may vary depending on your use case
    fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
});
  1. Run the file with:
node script.js # Here, script.js is the file created in the previous step

PS: If this is a recurring use case, then you could also turn the above script into a custom reporter (https://github.com/postmanlabs/newman#creating-and-using-custom-reporters)

@mpluta-airhelp

This comment has been minimized.

Show comment
Hide comment
@mpluta-airhelp

mpluta-airhelp May 16, 2017

I will use this to prepare a script for my use. Totally missed this functionality. However, I strongly suggest it would be a great function in your desktop app too.
But in the end great tip, thanks a lot!

mpluta-airhelp commented May 16, 2017

I will use this to prepare a script for my use. Totally missed this functionality. However, I strongly suggest it would be a great function in your desktop app too.
But in the end great tip, thanks a lot!

@kunagpal

This comment has been minimized.

Show comment
Hide comment
@kunagpal

kunagpal May 18, 2017

Member

@mpluta-airhelp Thanks for your suggestion, we're marking this issue as resolved. 😄

Member

kunagpal commented May 18, 2017

@mpluta-airhelp Thanks for your suggestion, we're marking this issue as resolved. 😄

@p00j4

This comment has been minimized.

Show comment
Hide comment
@p00j4

p00j4 Jul 16, 2018

@mpluta-airhelp while using postman app, in some of my requests, I want to write some of the key information. What changes I need to make to the approach you described above?
Basically, I want to be able to save

  • environment variable value(s)
  • variables generated in pre-reqeust scripts/test scripts [random id generated and used to make request]

because args.response and args.request both won't give me those

p00j4 commented Jul 16, 2018

@mpluta-airhelp while using postman app, in some of my requests, I want to write some of the key information. What changes I need to make to the approach you described above?
Basically, I want to be able to save

  • environment variable value(s)
  • variables generated in pre-reqeust scripts/test scripts [random id generated and used to make request]

because args.response and args.request both won't give me those

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment