5

I am attempting to run a script.js with newman from a locally saved postman collection. In postman the call works, and returns a response body token that I need access to.

I don't care how the response body is returned I just don't want to open postman if I don't have to.

I keep encountering an error of ReferenceError: responseBody is not defined

Any help on this matter would be really appreciated.

$ node script.js

var newman = require('newman'); // require newman in your project

// call newman.run to pass `options` object and wait for callback
newman.run({
    collection: require('./pathto/my_coll.postman_collection.json'),
    reporters: 'cli'
}, function (err) {
    if (err) { throw err; }
    // console.log(responseBody);
    JSON.parse(responseBody);

});

neither console.log or JSON.parse appear to be doing the trick because responseBody doesn't appear to be defined from the start

exhausted references:

https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox

https://www.npmjs.com/package/newman

how to get whole html or json repsonse of an URL using Newman API

RobBenz
  • 589
  • 1
  • 7
  • 21

3 Answers3

5

You could try console.log(summary.run.executions) and drill down into it from there. The Newman script doesn’t really know what responseBody is in that context so it wouldn’t know what to log out.

Check out the Newman docs for more information https://github.com/postmanlabs/newman/blob/develop/README.md#cli-reporter-options

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • thanks for the input. `ReferenceError: summary is not defined` – RobBenz Mar 29 '18 at 12:36
  • perhaps I am running `node` script from the wrong directory? – RobBenz Mar 29 '18 at 12:38
  • Is Newman installed globally? If it is, then it should be ok. Check out the docs and try out the different options to assess the different outputs. – Danny Dainton Mar 29 '18 at 12:41
  • https://stackoverflow.com/questions/40120232/javascript-from-buffer-to-json was helpful for converting the `execution.response.stream` to JSON/string – jmcker Oct 26 '20 at 07:21
4

A postman collection is a collection of requests.

You're running the whole collection (which is a series of requests being ran all together by Newman)

Thus, logging / parsing the responseBody in a callback function is incorrect (Stating this logically).

As per the Newman Docs it states that the .run function's callback is called with two parameters that are err and summary

The summary argument in the callback contains the whole summary for the run and you can follow the documentation if you want to make use of that summary.

Now, What you're trying to do is basically log the response of the request(s).

You need to write a console.log(responseBody) / JSON.parse(responseBody) inside the test scripts for each request in the collection and then on running the collection using newman, each responseBody for each request will be logged out / parsed as per your needs.

To access the summary you can modify your function like so :

var newman = require('newman');
newman.run({
    collection: require('./C1.postman_collection.json'),
    reporters: 'cli'
}, function (err, summary) {
    if (err) { throw err; }
    console.log(summary);
});
Sivcan Singh
  • 1,775
  • 11
  • 15
  • 2
    Thank you for a very detailed & informative answer. after adding `console.log(responseBody);` to my `Tests` tab and then RE EXPORTING my collection to local location. I was able to print the response to the shell. Thank you very much. – RobBenz Mar 29 '18 at 13:04
  • But how you can capture the response programmatically? I wan to run `Newman` command line from C# and capture the response to analyze it using. This will be part of NUnit test method. – tarekahf Mar 11 '22 at 21:27
3

Should be achiavble by parsing the buffer stream:

var newman = require('newman');
newman.run({
    collection: require('./C1.postman_collection.json'),
    reporters: 'cli'
}, function(err, summary) {
    if (err) {
        throw err;
    }
    summary.run.executions.forEach(exec => {
        console.log('Request name:', exec.item.name);
        console.log('Response:', JSON.parse(exec.response.stream));

    });
});
Abhinaba
  • 376
  • 1
  • 8