4

I need to be able to delete cookies automatically in between requests when they I run my collection of requests in Newman and Postman Runner (mainly Newman).

I followed the suggestion given in this comment by a person from Postman: https://github.com/postmanlabs/postman-app-support/issues/3312#issuecomment-516965288.

But it is not working.

The answer to these two SO questions also tell the same way to go about doing this: Postman: How do you delete cookies in the pre-request script? Deleting cookies in postman programmatically

Here is the code that I use that the sources above suggest to place in the pre-request script:

const jar = pm.cookies.jar();
jar.clear(pm.request.url, function (error) {
  console.log("Error: ");
  console.log(error);
//handle error
});

[Note: error is logged as null when I run this code]

I have tried this code many times and also many different modifications of that code. I do white-list the domain too. But I always get the wrong response in the request. When I clear the cookies manually (using the cookie Manager UI dialogue box), the request gives the right response. I need help in determining where the problem could be for me in deleting cookies programmatically.

I also tried this to see what the cookies that I am deleting are:

jar.getAll(pm.request.url, function (error, cookies) {
    console.log("Cookies:");
    console.log(cookies);
    console.log("Error: ");
    console.log(error);
});

Here cookies is an empty array. Perhaps that is the problem. But that is very weird since when I check Cookie Manager manually, there are many cookies shown. And once I delete the cookies manually the requests return the right responses.

Another question I had was: What is the purpose of the callback functions that take 'cookies' and 'error' as arguments in the code above. Are these functions called everytime or only under certain conditions? Could not find the purpose of the callback functions in the postman documentation: https://learning.postman.com/docs/postman/sending-api-requests/cookies/

Thank you

1 Answers1

3

If the cookie has "httpOnly" or "secure" header, you can't delete them via script in postman. jar.clear clears all the cookies except these httpOnly and secure ones.

I think this is a bug and needs to be fixed by Postman. If this is intended, there should be a setting in Postman to activate or disable it.

Emre
  • 337
  • 1
  • 3
  • 18