Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman: How do you delete cookies in the pre-request script?

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.

like image 587
K5 User Avatar asked Jun 20 '17 17:06

K5 User


People also ask

How do I remove cookies from Postman request?

navigate to View: Show DevTools. navigate to the Application tab, then the Clear Storage view in the left menu. deselect all choices except Cache Storage, then click on 'Clear site data' restart Postman.

Where are cookies stored Postman?

The cookies are information sent by the server and stored in the browser. As soon as a request is sent, the cookies are returned by the server. In Postman, the cookies are mentioned under the Headers and Cookies tab in the Response.

What is pre-request script in Postman?

You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data.


2 Answers

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>
});
like image 161
aaron Avatar answered Oct 19 '22 12:10

aaron


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

like image 24
Sergej Lopatkin Avatar answered Oct 19 '22 14:10

Sergej Lopatkin