How to fetch the synced cookies and set as environment var

I am currently syncing cookies from chrome using interceptor to postman and I can see the cookies being available in the postman. Let say the url is example.com to the website which is having 2 cookies XSRF-TOKEN and Authenticationtoken.
If I want to make a request to the api, I have to send the cookies along with that as a header XSRF-TOKEN must be passed with the name X-XSRF-TOKEN.

Since the cookies are already synced, I am trying to fetch the cookie and set it as environment variable so that the same can be utilized in the headers but unfortunately, the cookie value is not available in Pre-request Script but is available in Test.

var xsrfToken = pm.cookies.get("XSRF-TOKEN");
if (xsrfToken) {
    pm.environment.set("XSRFCookie", xsrfToken);
    console.log("XSRF Token stored:", xsrfToken);
} else {
    console.log("XSRF Token not found");
}

Can someone try to help me set this environment variable? My end motive is to use this collection in Runner.

Hey @aerospace-administr1

Have you tried using the pm.cookies.jar() function that’s listed further down in the learning center article?

You would need to add the domain to the allowlist before this will work within the scripts sandbox.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.