Collection Runner continues to iterate even when postman.setNextRequest(null)

How do I make the collection runner stop iterating based on the response code I received? I’m already using postman.setNextRequest(null) but the iteration still continues.

I’m trying to stop the iteration if the script gets a pm.response.code other than 200 or 204. Other than that, the script just writes the response info to a file (followed this doc).

Here’s my test script:

let resData = {
    httpResponse: pm.response.code,
    schemeId: pm.iterationData.get("id"),
    name: pm.iterationData.get("name")
};

if(pm.response.code === 200 || pm.response.code === 204) {
    resData.messages = ["Successfully deleted"];
}
else if (pm.response.code === 429) {
    resData.messages = ["Hit API Rate Limit"];
    postman.setNextRequest(null);
}
else {
    resData.messages = pm.response.json().errorMessages;
    postman.setNextRequest(null); // only adding this here for testing, ideally we only want to stop the iteration if we get a 429
};

pm.test("Succesful DELETE request.",function() {
    pm.expect(pm.response.code).to.be.oneOf([201,202,204]);
});

// The opts for the server, also includes the data to be written to file
let opts = {
    requestName: request.name || request.url,
    fileExtension: 'json',
    mode: 'appendFile', // Change this to any function of the fs library of node to use it.
    uniqueIdentifier: false,
    responseData: JSON.stringify(resData) + ","
};

pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(opts)
    }
}, function (err, res) {
    console.log(res);
});

This test script is only on the Collection level. There are no scripts on the Folder and Request levels. There are no pre-request scripts on any levels.

The request is only a simple delete request. My test iteration data should give out a 400 error on the first iteration and should not continue further but it still does.

I’ve already tried adding postman.setNextRequest(null) to different parts of the code. I found How to stop Collection Runner on error? - #15 by Merricat which suggested that I use postman.setNextRequest() to clear out the next request after first declaring it as null but that didn’t work either.

1 Like