# Set a cookie for a test

**URL:** <https://community.postman.com/t/set-a-cookie-for-a-test/8294>\
**Category:** Help Hub\
**Tags:** authentication, cookies\
**Created:** [October 10, 2019, 2:14pm UTC](https://community.postman.com/t/set-a-cookie-for-a-test/8294 "2019-10-10T14:14:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zap2Zero](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/zap2zero/32/15550_2.png) [@Zap2Zero](https://community.postman.com/u/Zap2Zero)\
**Post date:** [October 10, 2019, 2:14pm UTC](https://community.postman.com/t/set-a-cookie-for-a-test/8294/1 "2019-10-10T14:14:34Z")

</div>

I want to set a cookie for a test, which I try to do via a pre-request script.  
I have set an environment variable for the value I want to set (on multiple tests) and have this pre-request script:

> const cookieJar = pm.cookies.jar();  
> authcookie = pm.environment.get(“auth-cookie”);  
> console.log(authcookie);  
> cookieJar.clear(‘[https://myserver.com/](https://myserver.com/)’, function (error) { console.log(error); });  
> cookieJar.set(‘[https://myserver.com/](https://myserver.com/)’,  
> { “name”: “PHPSESSID”, “value”: authcookie, “httpOnly”: false },  
> function (error, cookie) { console.log(error); });

When I run the script, I get this error which I have no idea how to solve:

> Object {message: “CookieStore: programmatic access to “[myserver.com](http://myserver.com)” is denied”, name: “Error”, type: “Error”}
> 
> 1. message: “CookieStore: programmatic access to “[myserver.com](http://myserver.com)” is denied”
> 2. name: “Error”
> 3. type: “Error”

Any idea how to solve that?

Update: I am trying this in the Postman app on mac.

---

<div class="post-metadata">

**Author:** ![nachtmusik.laura](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nachtmusik.laura](https://community.postman.com/u/nachtmusik.laura)\
**Post date:** [November 15, 2019, 12:56am UTC](https://community.postman.com/t/set-a-cookie-for-a-test/8294/2 "2019-11-15T00:56:12Z")

</div>

Did you ever find a solution to your question?

---

<div class="post-metadata">

**Author:** ![Zap2Zero](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/zap2zero/32/15550_2.png) [@Zap2Zero](https://community.postman.com/u/Zap2Zero)\
**Post date:** [November 15, 2019, 5:48am UTC](https://community.postman.com/t/set-a-cookie-for-a-test/8294/3 "2019-11-15T05:48:03Z")

</div>

No, not really. I implemented a workaround where postman now has a workaround to authenticate itself so the session cookie can get setup using the regular way.

Zap.

---

<div class="post-metadata">

**Author:** ![rozagerardo](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rozagerardo/32/3709_2.png) [@rozagerardo](https://community.postman.com/u/rozagerardo)\
**Post date:** [January 29, 2020, 1:35pm UTC](https://community.postman.com/t/set-a-cookie-for-a-test/8294/4 "2020-01-29T13:35:33Z")

</div>

I solved this by following these instructions (" Whitelisting domains for programmatic access of cookies"):

[https://learning.getpostman.com/docs/postman/sending-api-requests/cookies/#whitelisting-domains-for-programmatic-access-of-cookies](https://learning.getpostman.com/docs/postman/sending-api-requests/cookies/#whitelisting-domains-for-programmatic-access-of-cookies)

Note: the ‘Whitelist Domains’ button might not be visible, I had to close a message about an Interceptor Lesson in the ‘Manage Cookies’ popup to see it.

After whitelisting your domain you’ll be able to manage the cookies.
