Json array handling different in Newman than Postman App?

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Javascript array.push command produces different results when run in Postman App vs Newman Command Line.

Right now I have tempBody defined as:

var tempBody = {};

• Then set to tempBody = pm.request.body; if request.body has content.
• Or set to tempBody = {"mode":"NoBody"}; if no request body.

Once set I create an element in the array as follows.

tempArray.push({"reqName":pm.info.requestName, "reqUrl":pm.request.url, "reqBody":tempBody});

I then add it as an updated environment value:
pm.environment.set("compareArray", tempArray);

In postman app, this statement:
console.log("my tempArray is : ", tempArray);

Produces the following, which includes the Body whether it exists or not. However, when I export and run this script in newman, the reqBody element is missing from the array of json objects.

See an example at the bottom.

Array:[]
	0:{}
	reqBody:{}
	mode:"urlencoded"
	urlencoded:[]
	0:{}
	key:"……."
	value:"……."
	1:{}
	key:"…….."
	value:"……."
	2:{}
	key:"…….."
	value:"……"
	reqName:"Get Token for Tests"
	reqUrl:{}
	host:[]
	0:"{{baseUrl}}"
	path:[]
	0:"v3"
	1:"tokens"
	query:[]
	variable:[]
	1:{}
	reqBody:{}
	mode:"NoBody"
	reqName:"FolderEnd"
	reqUrl:{}
	host:[]
	0:"{{baseUrl}}"
	path:[]
	0:"v3"
	1:"mymixchannels"
	query:[]
	variable:

When the same script is run in newman command line:

{
      "type": "any",
      "value": [
        {
          "reqName": "Get Token for Tests",
          "reqUrl": {
            "path": [
              "v3",
              "tokens"
            ],
            "host": [
              "{{baseUrl}}"
            ],
            "query": [],
            "variable": []
          },
          "reqBody": {
            "mode": "urlencoded",
            "urlencoded": [
              {
                "key": "……….",
                "value": "………."
              },
              {
                "key": "……….",
                "value": "………."
              },
              {
                "key": "………",
                "value": "…………"
              }
            ]
          }
        },
        {
          "reqName": "FolderEnd",

// ******* There is no "reqBody" here ***********

          "reqUrl": {
            "path": [
              "v3",
              "mymixchannels"
            ],
            "host": [
              "{{baseUrl}}"
            ],
            "query": [],
            "variable": []
          }
        }
      ],
      "key": "compareArray"
    },

Would you mind sharing the actual code?
It looks like reqBody is present in some of the newman output so I’m thinking it may be the condition checking if there’s a request body that may be incorrect.

Arlemi,
Thanks for the response. Below is a cut and paste of the PreRequest script. It is a prototype collection which has only two requests in it. The first Request has a Body. The second Request does not. The body is shown correctly in both Postman App and Newman/Nodejs console log for the first request which has a Body. However, for the second request which has no Body, I add a “dummy” body. The dummy body shows up in the Postman console log however, not in the Newman/Nodejs console log .

I updated the the output from the Postman console log and Newman/Nodejs console log to make sure it matches the prerequest script because I have tried multiple options which all end up with the same results.

Do Postman and Newman command line use the same package for parsing JSON? If you need to see the actual collection, please let me know how to upload and I will do that.

Thanks again,
Jill

POSTMAN PREREQUEST:

var mddbCollection = pm.variables.get(“testCollectionVariable”);

pm.environment.set(“requestAccept”, pm.variables.get(“responseContent”));

var tempArray = ;
var testArray = ;

if (!pm.environment.get(“compareArray”)){
console.log(“EMPTY”);
}
else {
console.log(“GET CONTENTS”);
tempArray = pm.environment.get(“compareArray”);
}

console.log(" stringify body", JSON.stringify(pm.request.body));
console.log(" stringify name “, JSON.stringify(pm.info.requestName));
console.log(” stringify url", JSON.stringify(pm.request.url));
var tempBody = {};

if (JSON.stringify(pm.request.body) === “{}”){
console.log(“EMPTY”);
tempBody = {“mode”:“NoBody”};
}
else {
console.log(“HAS A BODY”);

}

testArray.push({“reqBody”:tempBody, “reqName”:pm.info.requestName, “reqUrl”:pm.request.url});
console.log(“my testArray is : “, testArray);
console.log(” ***************************************”);

tempArray.push({“reqBody”:pm.request.body, “reqName”:pm.info.requestName, “reqUrl”:pm.request.url});
console.log(“my tempArray is : “, tempArray);
console.log(” ***************************************”);

pm.environment.set(“compareArray”, tempArray);
console.log(“my compareArray is : “, pm.environment.get(“compareArray”));
console.log(” ***************************************”);


POSTMAN PREREQUEST CONSOLE LOG:

GET CONTENTS

09:01:47.447

stringify body

09:01:47.450

{}

stringify name

09:01:47.453

“FolderEnd”

stringify url

09:01:47.455

{“path”:[“v3”,“mymixchannels”],“host”:["{{baseUrl}}"],“query”:,“variable”:}

EMPTY

09:01:47.456

my testArray is :

09:01:47.458

    • Array:
      • 0:{}
        • reqBody:{}
          • mode:“NoBody”
        • reqName:“FolderEnd”
        • reqUrl:{}
          • host:
            • 0:"{{baseUrl}}"
          • path:
            • 0:“v3”
            • 1:“mymixchannels”
          • query:
          • variable:

09:01:47.459

my tempArray is :

09:01:47.461

    • Array:
      • 0:{}
        • reqBody:{}
          • mode:“urlencoded”
          • urlencoded:
            • 0:{}
              • key:“grant_type”
              • value:"…"
            • 1:{}
              • key:"…"
              • value:"…"
            • 2:{}
              • key:"…"
              • value:"…"
        • reqName:“Get Token for Tests”
        • reqUrl:{}
          • host:
            • 0:"{{baseUrl}}"
          • path:
            • 0:“v3”
            • 1:“tokens”
          • query:
          • variable:
      • 1:{}
        • reqBody:{}
        • reqName:“FolderEnd”
        • reqUrl:{}
          • host:
            • 0:"{{baseUrl}}"
          • path:
            • 0:“v3”
            • 1:“mymixchannels”
          • query:
          • variable:

09:01:47.463

my compareArray is :

09:01:47.465

    • Array:
      • 0:{}
        • reqBody:{}
          • mode:“urlencoded”
          • urlencoded:
            • 0:{}
              • key:“grant_type”
              • value:"…"
            • 1:{}
              • key:“username”
              • value:"…"
            • 2:{}
              • key:"…"
              • value:"…"
        • reqName:“Get Token for Tests”
        • reqUrl:{}
          • host:
            • 0:"{{baseUrl}}"
          • path:
            • 0:“v3”
            • 1:“tokens”
          • query:
          • variable:
      • 1:{}
        • reqBody:{}
        • reqName:“FolderEnd”
        • reqUrl:{}
          • host:
            • 0:"{{baseUrl}}"
          • path:
            • 0:“v3”
            • 1:“mymixchannels”
          • query:
          • variable:

09:01:47.466


NEWMAN CONSOLE LOG:

│ 'my tempArray is : ‘, [ { reqName: ‘Get Token for
│ Tests’,
│ reqUrl:
│ { path: [ ‘v3’, ‘tokens’ ],
│ host: [ ‘{{baseUrl}}’ ],
│ query: ,
│ variable: },
│ reqBody:
│ { mode: ‘urlencoded’,
│ urlencoded:
│ [ { key: ‘grant_type’, value: ‘…’ },
│ { key: ‘username’, value: ‘…’ },
│ { key: ‘password’, value: ‘…’ } ] } }
│ ,
│ { reqName: ‘FolderEnd’,
│ reqUrl:
│ { path: [ ‘v3’, ‘mymixchannels’ ],
│ host: [ ‘{{baseUrl}}’ ],
│ query: ,
│ variable: } } ]
│ ’ ***************************************’
│ 'my compareArray is : ‘, [ { reqName: ‘Get Token
│ for Tests’,
│ reqUrl:
│ { path: [ ‘v3’, ‘tokens’ ],
│ host: [ ‘{{baseUrl}}’ ],
│ query: ,
│ variable: },
│ reqBody:
│ { mode: ‘urlencoded’,
│ urlencoded:
│ [ { key: ‘grant_type’, value: ‘…’ },
│ { key: ‘username’, value: ‘…’ },
│ { key: ‘password’, value: ‘…’ } ] } }
│ ,
│ { reqName: ‘FolderEnd’,
│ reqUrl:
│ { path: [ ‘v3’, ‘mymixchannels’ ],
│ host: [ ‘{{baseUrl}}’ ],
│ query: ,
│ variable: } } ]
│ ’ ***************************************’

Can you try replacing this condition

if (JSON.stringify(pm.request.body) === “{}”){

with

if (!pm.request.body){

If this doesn’t work, sharing your collection will be handy yes, you can get a shareable link to your collection from Postman, as explained on the following link:

https://learning.getpostman.com/docs/postman/collections/sharing_collections/#sharing-collections-in-the-app

if (!pm.request.body) {
had the same results. Both commands successfully determine that pm.request.body is null.

The issue is related solely to adding a properties to a JSON object then being able to print/log all the objects correctly from the Postman App.

When the collection is exported and run using Newman, the exact same statements successfully print the properties which were set to postman values HOWEVER NEWMAN DOES NOT PRINT
or show the properties in the JSON object which were set locally in the script. i.e.,

when:

  • tempBody = {“mode”:“NoBody”}; it does not show up in the newman log when the following statements are executed.

       testArray.push({"reqBody":tempBody, "reqName":pm.info.requestName, "reqUrl":pm.request.url});
       console.log("my tempArray  is :     ",  tempArray);
    

when:

  • those same properties are set to postman values (i.e, the pm.request.body exists) then when the following command is run the properties DO show up in the Newman Console log.

       testArray.push({"reqBody":pm.request.body, "reqName":pm.info.requestName,"reqUrl":pm.request.url});
       console.log("my tempArray  is :     ",  tempArray);
    

As is I can’t share the collection due to the collection containing passwords, etc. I will need to build a dummy collection before sharing.

Cause of the problem is found! Postman export is not setting “disableBodyPruning”: true

In my exported postman collection I found the following
“protocolProfileBehavior”: {}

I changed that parameter as per the issue suggestion above to:
“protocolProfileBehavior”: {
“disableBodyPruning”: true
}

npm list newman
shows:
newman@4.5.5

1 Like