Getting Bad-Request(400) while run Collection in CMD using Newman

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.

Here’s an outline with best practices for making your inquiry.

My question: I’ve tested some APIs in Postman and all the tests are passed and got 200 for each request. But when I tried to run the same collection in CMD, I’m getting 400 Bad Request. I used the link and also exported the collection to run it in CMD but it gives the same 400 bad request in both methods. is there any other method to run collections using newman?

Details (like screenshots):

How I found the problem:

I’ve already tried: used JSON link from the postman to run collection in CMD and used exported file to run collection in CMD

Your GET request doesn’t seem to be reading the variable correctly and is showing the placeholder.

Are you getting the variables from an environment file and are you including that in the Newman command line?

For example

newman run collection.json -e environment.json --reporters cli,junit --reporter-junit-export Results\junitReport.xml
2 Likes

I’m guessing this is it. You prob have ContactId set in your environment, but not in the environment you’re passing in the terminal.

I’d also suggest trying newman-reporter-htmlextra - npm. It generates a very detailed html report so you can look at all of the details of the request and response. It makes troubleshooting a lot easier.

1 Like

Thank you for the response.
I identified the issue here which is i am not getting the variable from Environment and i was trying to get it from global variables.
Then i tried to set an environment variable but it is not creating an environment variable . i attach the response and the script in this reply , could you please support me to find the issue here.

Response -
{
“EMPLOYER”: “MAS Holding”,
“CITIZENSHIP”: “18”,
“CITY”: “13”,
“CONTACTCATEGORY”: “12”,
“LASTNAME”: “Badewala”,
“ESD”: null,
“NAMEINFULL”: “Iruka”,
“PREFERREDLANGUAGE”: “10”,
“NIC”: “945687152V”,
“TITLE”: “7”,
“POSTAL_ADDRESS”: “33A, 5th Lane,Colpetty,Colombo 3”,
“STATEPROVINCE”: “15”
}

Script -

pm.test(“Getting Contact Data by ID”, () => {
pm.response.to.have.status(200);
});

const getNic = pm.response.json();
pm.environment.set(“NicGlob”, getNic.NIC);
//pm.globals.set(“NicEnv”, getNic.NIC);
console.log(getNic.NIC); //getting the NIC correctly

Thank you.
Yes i noticed the mistake and i was tried to getting the variable from global’s which is not correct.
I also generated htmlextra reports for troubleshooting .
But right now i’m getting another issue which is cannot set an environment variable using response data.

I can’t really see anything wrong with your code.
Can you confirm that this is a string with a single entry?

response = pm.response.json()

pm.test('Test NIC is a string', () => {
    console.log(response.NIC);
    pm.expect(response.NIC).to.be.an('string');
    pm.globals.set('NIC', response.NIC);
});
1 Like

Thank you very much again, i really appreciate your support regards this manner and i found the issue with it. i made a mistake in the beginning “Didn’t create an environment variable to store my variables”.
Now it works smoothly. :innocent: