Search code examples
node.jsjsonpostmanpostman-collection-runnernewman

How to get the JSON response from newman


I tried to run the end point which is running fine on postman and I just exported as collection and running it through newman on JENKINS CI.

Command:

   newman run <POSTMAN_COLLECTION>.json -r json,cli

I'm getting the response.json file in the current directory as like below file:

enter image description here

I'm not able to see the response body inside the json file.

I'm googled but no luck. Is there anyway to get the response body for this postmand_collection? how can I achieve this?

I just want to get the response body as json file and I need to use it as request for other service.


Solution

  • You could create a mini project and use Newman as a library and run it using a script. That way you could use the node fs module to write the response out to a file.

    const newman = require('newman'),
            fs = require('fs');
    
    newman.run({
        collection: '<Your Collection>'
    }).on('request', function (error, data) {
        if (error) {
            console.error(error);
        }
        else {
            fs.writeFile(`response.json`, data.response.stream.toString(), function (error) {
                if (error) { 
                    console.error(error); 
                }
            });        
        }
    });
    

    This script is using the Newman .on('request') event, that will extract that information. If you wanted all the response bodies, you may need to modify this slightly and maybe use the appendFileSync to capture all the responses from the requests in the collection.