profile
viewpoint

Ask questionsDelete cookies programmatically

I'm looking for a way to clear cookies between each test, I know I am able to do this via the GUI but I want to do it automatically in the pre-request scripts. I tried document.setCookie = ..., but document isn't defined.

My current work around is to set the cookies into global variables, and use these variables in the header values, which allows me to set the values to empty strings at the beginning of each test.

postmanlabs/postman-app-support

Answer questions codenirvana

We've added this on our Canary (v7.3.5-canary04) channel https://www.getpostman.com/canary

We are currently working on documenting this feature and the sandbox APIs. Meanwhile, here's the brief to get started with this on Canary:

Whitelisting Domains

First, you need to whitelist the domains for which you want programmatic access to be enabled. Add domains under the new "Whitelist Domains" settings (bottom left of the existing cookie manager).

1 2

Pro Tip: To whitelist a domain and all its subdomains, use *.domain.com

Sandbox APIs

We added support for set, get, getAll, unset, and, clear methods on cookies for the given URL. All these methods are accessible using the new CookieJar pm API.

Accessing the CookieJar

const jar = pm.cookies.jar();

Note: All the CookieJar methods are asynchronous. It throws access denied error for non-whitelisted domains cookie access.

CookieJar.set There are three ways to set a cookie.

const jar = pm.cookies.jar();

// using cookie name and value
jar.set('http://example.com', 'token', '123456', function (error, cookie) {
  // error - <Error>
  // cookie - <PostmanCookie>
  // PostmanCookie: https://www.postmanlabs.com/postman-collection/Cookie.html
});

// using set-cookie string
jar.set('http://example.com', 'token=123456; Path=/; HttpOnly;', function (error, cookie) {
  // error - <Error>
  // cookie - <PostmanCookie>
});

// using PostmanCookie or its compatible object
jar.set(pm.request.url, { name: 'token', value: '123456', httpOnly: true }, function (error, cookie) {
  // error - <Error>
  // cookie - <PostmanCookie>
});

CookieJar.get Get the cookie value with the given name.

const jar = pm.cookies.jar();

jar.get('http://example.com', 'token', function (error, value) {
  // error - <Error>
  // value - <String>
});

CookieJar.getAll Get all the cookies for the given URL.

const jar = pm.cookies.jar();

jar.getAll('http://example.com', function (error, cookies) {
  // error - <Error>
  // cookies - <PostmanCookieList>
  // PostmanCookieList: https://www.postmanlabs.com/postman-collection/CookieList.html
});

CookieJar.unset Remove single cookie with the given name.

const jar = pm.cookies.jar();

jar.unset(pm.request.url, 'token', function (error) {
  // error - <Error>
});

CookieJar.clear Remove all the cookies for the given URL.

const jar = pm.cookies.jar();

jar.clear(pm.request.url, function (error) {
  // error - <Error>
});

Pro Tip: Use pm.request.url for current request URL

Everyone, please try it out and share the feedback. Will update the thread once we release this on our stable channel.

useful!
source:https://uonfu.com/
Github User Rank List