Postman CLI Creating cookies via test scripts does not work

I have a collection where i perform a login . In order to login i create a cookie from the test scripts but when i run the collection from postman cli , powershell it fails. The same collection when run from postman it works ok.

Hey @science-astronaut-33 :wave:

Welcome to the Postman Community! :postman:

Could you expand more on how that cookie is captured and stored?

Whatโ€™s the CLI command that youโ€™re using?

Yes so i use the following command to run my collection ( this collection works and passes all test from collection runner)
postman collection run {id} -e {id} --verbose
the only i do in test scripts is to get a value from a response header and then create a cookie for it

The first request script >

>// Get the session Id value from the set-cookie header
var sessionId = pm.response.headers.find(h => h.key === 'Set-Cookie').value.match(/SessionId=([^;]+)/)[1];

// create a cookie
var cookieDomain = pm.environment.get('baseUrl_1').replace("https://", "");
cookieDomain = cookieDomain.replace(/\/$/, ""); // Remove the trailing slash

Second request script

pm.test("Creating SessionId cookie", function(){
    // Extract SessionId from response headers
var sessionIdMatch = pm.response.headers.find(h => h.key === 'Set-Cookie').value.match(/SessionId=([^;]+)/);
if (sessionIdMatch) {
    var sessionId = sessionIdMatch[1];
    pm.environment.set('SessionId', sessionId);

    // Remove trailing slash from baseUrl_1
    var baseUrl = pm.environment.get('baseUrl_1').replace("https://", "");
    baseUrl = baseUrl.replace(/\/$/, ""); // Remove the trailing slash

    // Set the cookie
    const cookieJar = pm.cookies.jar();
            name: 'SessionId', 
            value: sessionId,
            domain: baseUrl, // Set domain without paths or slashes
            httpOnly: true 

Problem i face is that no cookies are captured when i send the second request. Also the one i am creating i can not see it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.