Deleting cookies in postman programmatically

2020-08-17 05:31发布

问题:

I'm using Newman and the native Windows Postman app to test a REST API. It stores the session cookie between requests, allowing me to access information that requires authorization without providing correct authorization. I would like to be able to delete the cookie within the pre-request script section. Is this possible? I know how to delete cookies using the GUI through reading questions such as How to delete session cookie in Postman? and the official postman documentation but that doesn't help me deal with this issue.

回答1:

Postman v7.6.0 has added support for programmatic cookie access. Hence, if you want to delete a cookie in the pre-request script, you can do the following:

Remove a single cookie

const jar = pm.cookies.jar();

jar.unset(pm.request.url, 'cookie name', function (error) {
  // handle error
});

Remove all cookies

const jar = pm.cookies.jar();

jar.clear(pm.request.url, function (error) {
  // handle error
});

You can find a detailed overview of the API here: https://learning.getpostman.com/docs/postman/sending-api-requests/cookies/#programmatic-accees-of-cookies



回答2:

This is not currently possible with Postman, it's an open feature request at the moment:

https://github.com/postmanlabs/postman-app-support/issues/3312#issuecomment-413750185