25
votes

All the postman cookie-management answers I've seen refer to either the browser extension (open chrome, delete cookies viz interceptor etc) or with the app, using the UI to manually manage cookies.

I would like to delete certain cookies in my pre-request code as part of scripting my API tests. (delete them programmatically)

The Sandobx API docs mention pm.cookies so I tried

if (pm.cookies !== null) {
   console.log("cookies!");
   console.log(pm.cookies);
}

But the pm.cookies array is empty. Yet in the console, the GET call then passes a cookie.

There's also postman.getResponseCookies, which is null (I assume because we're in the pre-request section, not in the test section)

One answer suggested calling the postman-echo service to delete the cookie. I haven't investigated this yet, but it doesn't feel right.

3
(This is specific to the postman app, not the extension, sorry if I wasn't clear) - K5 User
If your API has a delete function that you can use for cookies (like postman-echo has), you may build one request to delete them and chain the request you want to execute (via setNextRequest()). - A.Joly
The answer concerning postman echo is a dead end, cookies are linked to a domain, so your cookie will be linked to yours, not postman-echo, and then won't be deleted. Too bad, it actully deleted the cookie from the cookie manager. - A.Joly
You may have a look here stackoverflow.com/questions/1085756/…, but if your cookie is set with http_only, you won't be able to access (and remove) it. In my case, it is http_only (sob). Else you can change its date (accessing it in javascript) and set it to yesterday or even -1 could work I think ... - A.Joly
There is a feature request for this: github.com/postmanlabs/postman-app-support/issues/3312 - Suzana

3 Answers

5
votes

new version now supports that since 2019/08, see more examples here: Delete cookies programmatically · Issue #3312 · postmanlabs/postman-app-support

Prerequisite

Cookie domains to be given programatic access must be whitelisted.

clear all cookies

const jar = pm.cookies.jar();

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

get all cookies

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
});

get specific cookie

const jar = pm.cookies.jar();

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

According to the documentation pm API reference the pm.cookie API is only for the Tests tab, not for the Pre-request Script.

The following items are available in TEST SCRIPTS only.

pm.cookies

...

It seems that you will have to stick with this method : Interceptor Blog post

-1
votes

I know this is a very late answer, but for my case where I didn't want to use the cookies to start the execution of the collection, I just needed to uncheck the option "Save cookies after the collection run" and check the option "Run collection without using stored cookies" on the Runner panel.

enter image description here

And then if I want to manage the cookies on my own, I created a first request on the collection and used the Tests tab just to collect the cookies that I wanted and saved them on a variable.

pm.environment.set('cookie', pm.cookies.get('csrftoken'))
pm.environment.set('sessionid', pm.cookies.get('sessionid'))