I am creating a series of API calls in Postman, I am then putting together a C# script to run them through Newman and the Postman API in a specific order, one of my collections contains a call that I need to run several times depending on the result of another.
I have this set up in postman, and everything there seems to work fine, if I select to run the collection with x iterations it works perfectly, similarly if I press the send button manually multiple times it works fine.
The code that I am using is essentially the same code that is used in the postman example for a multiple Post query. I only really changed the names to be suitable for what I am using, the pre-request script is as follows:
let camNames = pm.collectionVariables.get("camNames");
if(!camNames || camNames.length == 0) {
camNames = pm.environment.get("CamNamesFromRequest");
}
let currentCamName = camNames.shift();
pm.collectionVariables.set("camName", currentCamName);
pm.collectionVariables.set("camNames", camNames);
and the Test script is this:
const camNames = pm.environment.get("camNames");
if (camNames && camNames.length > 0){
postman.setNextRequest("Loop Query Parameter");
} else {
postman.setNextRequest(null);
}
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
However whenever I try to run it thorough Newman all of the tests fail with this
 MultipleAddCameras
9Ôáä TypeError in prerequest-script
POST https:/IPADDRESS/I/SERVICEIAMUSING/api/Objects/PersistObject [401 Unauthorized, 432B, 82ms]
10. Status code is 200
Not sure how to address this, if anyone has any suggestions?
I have tried fiddling about with the variables, but I am not really sure where to start since I just borrowed the code from the Postman example, I would have a clearer idea if the Postman script wasn't working, but it is... which is what has me confused...