Running the tests in Newman CLI gives 400 Bad Request

Hi guys. I tried different solutions but nothing seems to work. I’m a beginner in Postman/Newman collections. My end goal is to have a working solution with Newman being integrated with Jenkins.

So, I have the collection and a “Pre-request Script” where I basically handle random Strings/Numbers to be assigned to a Global Variable, something like this:

const dateNow = new Date();
pm.globals.set("currentDate", dateNow.toISOString());
pm.globals.set("randomNumber", _.random(1,9999));
function randomString(minValue, maxValue, dataSet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    if (!minValue) {
        minValue = 8;
        maxValue = 8;

    if (!maxValue) {
        maxValue = minValue;

    let length = _.random(minValue, maxValue),
        randomString = "";

    for (let i = 0; i < length; i++)
        randomString += dataSet.charAt(Math.floor(Math.random() * dataSet.length));
    return randomString;

pm.globals.set("randomPincode", randomString());

As you can see in the following screenshot the variables are correctly defined:

Then, in the request, I just call that variable and the value will be stored correctly in the Global Variable.

    "momentOfOrigin": "{{currentDate}}",
    "eventId": {{randomNumber}}

So, running directly from the Postman works fine, but the issue occurs when running it from Newman CLI.
I already exported those global variables as JSON and when running from the command line I’m using: newman run Collection.json -g global_variables.json

As a result, all the requests are having the status 400 Bad Request. I guess something goes wrong in defining the variables but I don’t know what.

I don’t know if this is causing it, but your initial values should be a value, not the variable name.

I had no idea what that will do, as its like calling itself.

It should be a value or just left blank.

Tried that already and gives the same error.
Tried with Initial Value empty, same values as the “Current value” and with the variable name itself. Nothing worked.

Console log the variables in the pre-request script, and the response, so you can see what is being sent and received, etc.

Put a pm.globals.get just after each pm.globals.set to make sure they are being set.

You should be able to see these being returned in the Newman console.

Is it reading any of the global variables?

If your currentDate, randomNumber and randomPincode is being generated by the pre-request script, then they don’t really need to be defined in the globals export. As they will be generated on runtime.

I added what you said and even an unset before setting the actual variable.
So now looks something like this:

pm.globals.set("currentDate", dateNow.toISOString());
var currentDate = pm.globals.get("currentDate");

In the response, when sending from Postman the currentDate and eventId are filled in correctly with random values:


That console.log seems to return the correct value:

And same in Newman:


And just to be sure, this is my request: this is how the variables should be called, right?


Sorry, you need to wrap this in the console log.


Do the same for the other variables.

The following is a JSON object set as the body? (In which case, yes, this is how you target the variables).


You might want to try the momentOfOrigin without the quotes (and see if that makes any difference). It should already be stored as a string.


The right values seem to be there but requests from Postman are still failing.

It’s weird though as Newman says that the “prerequest-scripts” are just fine and were executed successfully.
But there is something wrong with the “test-scripts”? But if so, why doesn’t fail in Postman directly?

What is being sent in that post request?

Does it appear correct? With the correct format and values being sent?

I can only assume at this moment that the post request is missing something. Just need to work out what is missing. Is it missing a particular value or all of the values?

Well, again, same request from Postman it’s working just fine and the correct values are sent.

The problem is when running via CLI. (I also tried via Postman CLI and it’s the same outcome).

The request looks something like this:

    "momentOfOrigin": "{{currentDate}}",
    "eventId": {{randomNumber}},
    "p": {
        "p": {
            "b": "bar"
        "action": {
            "type": 10,
            "do": "{{currentDate}}"
        "bl": {
            "rB": 1,
            "des": "Description"
        "location": {
            "des": "{{randomLocationDescription}}",
            "servi": "{{randomServiLocker}}"
        "del": {
            "pincode": "{{randomPincode}}"
        "communication": {
            "subject": "Subject communication",
            "longMessage": "Long message",
            "shortMessage": "Short message"
    "cl": "string",
    "eventVersion": "event version"

I just replaced the real fields with some other values, but this is the core.

Ok, you have a few variables in that body.

So my previous question\observation still applies.

It would appear that the POST request is not sending the correct information.

So you need to check the POST request in Newman and ensure the body is being sent correctly, and each of the variables is being transposed correctly.

The post request in the console logs should be the same for Postman and Newman.

Is the problem with a single value not being set, or none of the values being set correctly.

It should work the same in Postman and Newman but make sure the variables are being transposed to the value, and not showing for example the name of the variable as a string. I would also recommend removing the double quotes from around the variables in the body to rule that out as they shouldn’t be needed, as they are stored as strings when saved as a global variable.

All the variables are transposed correctly to values, as you can see above.

I can’t see anything wrong with that. It looks like the values are being transposed correctly.

What do you have in params and settings?

Do you have any SSL settings?

Are the headers the same in Postman and Newman? (Again, you should be able to see the request in the console to confirm the two are the same).

Solved it in the end. You were absolutely right about something that’s wrong with the request.
There were some “//comments” left there for hiding a field. I know JSON doesn’t allow comments but seems that Postman allows you to have them in your request and doesn’t take them into consideration when sending the actual request. Which Newman/Postman CLI don’t.
Thanks a lot for debugging this with me @mdjones.

1 Like