Pm.cookies.get('csrftoken') getting undefined

Hello, I’ve been struggling to get the csrftoken cookie. My use case is I’m trying to set the X-CRSFToken header to the csrftoken cookie value before sending the request. It’s just a regular cookie that is stored in the cookie manager. I have added my domain to the allowlist, tried using cookie jar, interceptor to no avail. Although, the cookie is marked as Secure so I’m not sure if the impacts the ability to get the cookie. Here’s what I’m trying.

In pre-request scripts:

var csrfToken = pm.cookies.get('csrftoken');

console.log(csrfToken);
// If the cookie exists, set the X-CSRFToken header
if (csrfToken) {
    pm.request.headers.add({key: 'X-CSRFToken', value: csrftoken.value});
}

However, csrftoken is undefined in console.

Here’s proof that the cookie is indeed in the cookie manager:

Header:

Allowlist:

I have tried adding interceptor as well, but it didn’t change anything.
I must be missing something obvious. I simply just want to retrieve the csrftoken cookie and set the X-CSRFToken header to that value before sending the request.

Have you tried using localhost instead of the IP address?

Have you tried pulling back all of the cookies?

I can’t see pm.cookies.get in the Postman documentation. I can see some example of it on the web, so a bit surprised. Has this been deprecated?

Using cookies | Postman Learning Center

It talks about retrieving using CookieJar. Although I can’t tell if that is for all Cookies, or just the ones you create.

For example. To get all Cookies would be the first thing I would try.

The following seems to work.

const cookieJar = pm.cookies.jar();
const url = 'www.myurl.com'
cookieJar.getAll(url, (error, cookies)=> {
    if (error) {
        console.log(error);
    } else {
        console.log(cookies);
    }
});