semih
semih

Reputation: 45

Pre-request script doesn't change the collection variable using Postman

Pre-Request Script:

let user_id = pm.collectionVariables.get("user_id");
pm.sendRequest(`http://security.postman-breakable.com/account/${user_id}/summary`, function (err, response) {
    if(response.status == "FORBIDDEN"){
        pm.collectionVariables.set("status_code", 403);
    }else if(response.status == "OK"){
        pm.collectionVariables.set("status_code",200);
    }
});

Test:

let status_code = parseInt(pm.collectionVariables.get("status_code"));
pm.test(`Status code is ${status_code}`, function () {
    pm.response.to.have.status(status_code);
});

The response code is 200 but it reads the previous response code which was 403.

Although I try to change my collection variable called "status_code" by writing pre-request script when the response code changes, it doesn't change.

Upvotes: 0

Views: 2083

Answers (3)

Teodora Levkova
Teodora Levkova

Reputation: 11

I have a similar problem. I send a request in collection level pre-request script, save a value from the response in a collection variable (in my case the value is the entire request body stringified) and then try to get the value in the request's pre-request script.

Since this is a variable that I use in other places in my collection, I first unset it and then save the new one just in case. In the collection pre-request script I added a log with the collection variable value and it contains the expected request body.

All good so far, but when I manually send the request that reads the same variable in the pre-request script, it holds the data of a previously sent request. I first thought that the pre-request script request takes too long and it proceeds to request execution before the value is saved but when I added a wait, I see in the logs that the variable value is correct.

I have no idea why it takes previous request data. The most shocking part is that the same stale data remains even when I execute the request multiple times (again, I see in the logs when I get the collection variable that the value is changed). When I remove the variable from the collection and execute only this request, all works as expected.

EDIT: After trying a few more things to see what may causes the issue, it looks like that it only reproduces when the collection variable is set within the scope of the pm.sendRequest function. When it's set outside of it, the value is stored successfully.

The problem remains but there is a workaround at least.

Upvotes: 0

Wayne Smith
Wayne Smith

Reputation: 56

I can replicate this 100% of the time. Searching for a solution brought me here.

I truly think it's a bug: it seems that Pre-request Scripts (in 10.8.7) get either a shallow copy of whatever state Collection Variables were in at time of Send, or the binding between pm.collectionVariables and Collection Variables has broken. I can't imagine that this behavior is intentional.

For now, I would suggest that you refactor this into a Request and have code in a Test do the pm.collectionVariables.set()

The following doesn't apply to your specific use case, but perhaps it will be relevant to whomever finds this question before the bug is fixed: for my own use I built a utility object with a function to manipulate a "cleanup" list in Collection Variables, but changes wouldn't persist. The workaround for my case is to pass the pm.collectionVariables reference from the Test sandbox at call-time, since that instance seems bound to Collection Variables in the UI.

My Collection's Pre-request Script:

utils = {
    addCleanup: (collection, route, id) => {
        const list = JSON.parse(collection.get('test_cleanup_list'));
        list.push({ route, id, id_field: `${route}id`});
        collection.set('test_cleanup_list', JSON.stringify(list));
        return list;
    }
};

and then from a Test in a Request later in the Run:

utils.addCleanup(pm.collectionVariables, 'account', account.id);

Upvotes: 4

Paulo Fernando
Paulo Fernando

Reputation: 3660

I was able to reproduce a similar behaviour, any chance you did something like this? Where the pre-request script calls a different endpoint than the actual request?

enter image description here

Also, don't know if it can help, in my postman the Forbidden was not uppercase

Upvotes: 0

Related Questions