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

**URL:** <https://community.postman.com/t/pm-cookies-get-csrftoken-getting-undefined/46938>\
**Category:** Help Hub\
**Tags:** cookies\
**Created:** [April 30, 2023, 11:32am UTC](https://community.postman.com/t/pm-cookies-get-csrftoken-getting-undefined/46938 "2023-04-30T11:32:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mission-observer-114](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@mission-observer-114](https://community.postman.com/u/mission-observer-114)\
**Post date:** [April 30, 2023, 11:32am UTC](https://community.postman.com/t/pm-cookies-get-csrftoken-getting-undefined/46938/1 "2023-04-30T11:32:34Z")

</div>

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:

```auto
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:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/e/eea940e7fab8a7f9546d641b34e3364e4dc37753.png)

Header:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/9/b90e83613a74bd387c664d75f27591864c11143b.png)

Allowlist:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/9/49fa2293c04fcf034ff42d0d9772a307a47bed11.png)

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.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [May 2, 2023, 9:52am UTC](https://community.postman.com/t/pm-cookies-get-csrftoken-getting-undefined/46938/3 "2023-05-02T09:52:57Z")

</div>

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](https://learning.postman.com/docs/sending-requests/cookies/#accessing-cookies-in-scripts)

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.

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

```
