3

Is there a way to retrieve the response cookie names in a Postman test? I can get values with postman.getResponseCookie("<COOKIENAME>").value. However, based on differing scenarios, the cookiename changes.

fil
  • 137
  • 1
  • 9
  • Does this answer your question? [Get and store the value of a cookie using Postman](https://stackoverflow.com/questions/49259402/get-and-store-the-value-of-a-cookie-using-postman) – BigButovskyi Sep 08 '21 at 19:08

3 Answers3

3

Got it! I evaluated responseCookies to determine the cookie name.

fil
  • 137
  • 1
  • 9
3

You can check for a specific cookie in a test like this:

pm.test("Response contains a JSESSIONID cookie", function() {
    pm.expect(pm.cookies.has('JSESSIONID')).to.be.true;
});

This works for me in Postman v7.3.6 without the Postman interceptor turned on.

Brian De Sousa
  • 458
  • 5
  • 11
0

If you are looking for response cookie, and not for current cookie, better check it from response header (set-cookie).

Example

pm.test('Response has correct cookie', function () {
    // Get response cookies from header.
    // Response can have multiple set-cookie headers.
    // Variable i is an Header object which has property key and value.
    const cookies = pm.response.headers.filter((i) => i.key === 'set-cookie');
    // Make sure that at least have 1 set-cookie header.
    pm.expect(cookies).to.not.have.lengthOf(0);
    // Get cookie value and extract the name: (position 0 before =).
    const cookieValues = cookies.map((i) => i.value.split('=')[0]);
    // Make sure the length is the same.
    pm.expect(cookieValues.length).to.equal(cookies.length);
    // Make sure to have specific cookie name.
    pm.expect(cookieValues).to.includes('somecookiename');
    pm.expect(cookieValues).to.includes('JSESSIONID');
  });

References: Postman Response, Header and HeaderList documentation.

andreyunugro
  • 1,096
  • 5
  • 18