3

In postman, I have a pre-request scenario where I have to take a cookie value and store it in the environment variable, say csrf in my case, and use it for the API request. As shown in the image, I have 2 cookies for the domain namely XSRF-TOKEN and _d. I have to take only the XSRF-TOKEN for the particular domain and store it in csrf variable. I have to exclude Path, Domain and the Secure fields

My pre-request script:

pm.environment.set("csrf", pm.cookies.get('XSRF-TOKEN'))

Postman_Screenshot_Cookie

With this, the environment variable csrf is stored as null, and I don't understand where I have gone wrong. Any help is appreciated.

Rohit R
  • 41
  • 1
  • 3

1 Answers1

2
const jar = pm.cookies.jar()

jar.get('example.com', 'cookieName', (err, cookie) => {
    console.log(cookie)
})

See: https://learning.postman.com/docs/postman/sending-api-requests/cookies/#get-a-cookie

You'll also need to whitelist the domain you're trying to get the cookie from: https://learning.postman.com/docs/postman/sending-api-requests/cookies/#whitelisting-domains-for-programmatic-access-of-cookies

Matt
  • 720
  • 8
  • 15