Postman not sending request with `pm.sendRequest` if cookie jar is empty

Postman is not sending request with pm.sendRequest if cookie jar is empty.

I run a Test after every request in a collection, to check if the request was authorized. If not i get the necessary cookie and set it.

  • This works if the cookie is invalid but already exists in the cookie jar.
  • If the cookie does not exists already the code runs like expected but pm.sendRequest is not execute at all. No log inside the request function in the console (i use logs to check if the code part is reached), no request visible in the console. But the other logs are still printed. Neither do i get an error.

Postman is not sending request with pm.sendRequest if cookie jar is empty.

I run a Test after every request in a collection, to check if the request was authorized. If not i get the necessary cookie and set it.

  • This works if the cookie is invalid but already exists in the cookie jar.
  • If the cookie does not exists already the code runs like expected but pm.sendRequest is not execute at all. No log inside the request function in the console (i use logs to check if the code part is reached), no request visible in the console. But the other logs are still printed. Neither do i get an error.

The code:

if (pm.response.status == "Unauthorized") {

var domain = pm.environment.get("domain");
const jar = pm.cookies.jar();
jar.unset(domain, "session");

const postRequest = {
    url: 'http://' + domain + ':5000/api/v1/oauth2/token',
    method: 'POST',
    header: {
        'Content-Type': 'application/json',
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
            "identification": "admin",
            "password": "admin"
        })
    }
};
console.log("before sending") //**guard one - reached every time**
pm.sendRequest(postRequest, (error, response) => {
    console.log("sending") //**guard two - only reached if cookie exists but is invalid**
    if (error) {
        console.log("ERROR");
        console.log(error);
    }

    else {
        console.log("setting token via request")
        let token = response.json().token;

        jar.set(domain, "session", token, (err, cookie) => {
            if (err) console.log(err);
            else console.log("COOKIE:", cookie)
        })
    }
})
console.log("after sending") //**guard three - reached every time**

}