'How to get all Set-Cookie headers with Postman

My app returns two Set-Cookie headers: JSESSIONID and AWSELB.

When I write test in Postman and use postman.getResponseHeader("Set-Cookie") it only returns AWSELB.

Any idea how can I get JSESSIONID?

EDIT: Accepted Answer solved it in one way, now I have same issue but with sending two headers with same key. I should be able to send multiple 'Set-Cookie' headers, but when I do that it looks like only the last one is being sent, first one is overridden.



Solution 1:[1]

Actually postman contains all headers under postman.response.headers Its type is HeaderList. But it stores headers with type Array. And Header has key and value.

So you can loop through postman.response.headers and filter out what you need with either value or key

//filter by header key

pm.response.headers
.filter(header=>header.key.includes("whatever you are looking for"))
.map(f=>console.log( f.value))

//filter by header value

pm.response.headers
.filter(header=>header.value.includes("whatever you are looking for"))
.map(f=>console.log( f.value))

Solution 2:[2]

i used below method to get cookies from response header,

const Cookie = require('postman-collection').Cookie;

const oResponseHeaders = pm.response.headers;
oResponseHeaders.filter(function(resHeader){
    //console.log("resHeader : ",resHeader);
    
    let bSetCookieExists = resHeader.key.includes("Set-Cookie");
    if(bSetCookieExists){
        console.log('cookie : ',resHeader.key.includes("Set-Cookie"));
        
        let  rawHeader = pm.response.headers.get("Set-Cookie");
        let myCookie = new Cookie(rawHeader);
        console.log("myCookie : ",myCookie.toJSON());
        console.log("myCookie name : ", myCookie.name);
        console.log("myCookie value : ", myCookie.value);
      
    }
    
   
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 luckystones
Solution 2 Arrow