Search code examples
reportpostmanresponsenewman

Response of Requests made shown as empty in json report of newman


After running the collection using newman with json reporter, json file gets generated.

But for response portion it is [] i.e. empty, while it has different response related attributes with proper values (e.g. for responseTime, responseSize, etc).

So how can I get the response body/data in this json reporter.

As per my actual requirement, I need to record response for each request made in either json or excel/csv format file.


Solution

  • While I was not able to solve this problem directly, I used Newman as a javascript library and recorded the request and response in separate text files.

    The files generated will have file names like request1, request2 and so on for request files; and similar behavior will be for response files and their names for every execution.

    Below is the code for the above mentioned solution:

    const newman = require('newman'),
          fs = require('fs');
    var rq = 1;
    var rs = 1;
    
    newman.run({
        collection: require('./ABC.postman_collection.json'),
        environment: require('./XYZ.postman_environment.json'),
        iterationData: './DataSet.csv',
        reporters: 'cli'
    }).on('beforeRequest', function (error, args) {
        if (error) {
            console.error(error);
        } else {
            fs.writeFile('request' + rq++ + '.txt', args.request.body.raw, function (error) {
                if (error) { 
                    console.error(error); 
                }
            });    
        }
    }).on('request', function (error, args) {
        if (error) {
            console.error(error);
        }
        else {
            fs.writeFile('response' + rs++ + '.txt', args.response.stream, function (error) {
                if (error) { 
                    console.error(error); 
                }
            });        
        }
    });