> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/grafana/k6/llms.txt
> Use this file to discover all available pages before exploring further.

# Cookies and Sessions

> Handle cookies and maintain sessions in k6

## VU Cookie Jar

By default, each Virtual User (VU) maintains its own cookie jar that automatically handles cookies:

```javascript theme={null}
import http from "k6/http";
import { check, group } from "k6";

export let options = {
    maxRedirects: 3
};

export default function() {
    group("Simple cookies set with VU jar", function() {
        // Since this request redirects the `res.cookies` property won't contain the cookies
        let res = http.get("http://httpbin.org/cookies/set?name3=value3&name4=value4");
        check(res, {
            "status is 200": (r) => r.status === 200
        });

        // Make sure cookies have been added to VU cookie jar
        let vuJar = http.cookieJar();
        let cookiesForURL = vuJar.cookiesForURL(res.url);
        check(null, {
            "vu jar has cookie 'name3'": () => cookiesForURL.name3.length > 0,
            "vu jar has cookie 'name4'": () => cookiesForURL.name4.length > 0
        });
    });
}
```

<Note>
  Cookies set by the server (via Set-Cookie headers) are automatically stored in the VU's cookie jar.
</Note>

## Sending Cookies with Requests

Send cookies as part of a request:

```javascript theme={null}
import http from "k6/http";
import { check, group } from "k6";

export default function() {
    group("Simple cookies send with VU jar", function() {
        let cookies = {
            name: "value1",
            name2: "value2"
        };
        let res = http.get("http://httpbin.org/cookies", { cookies: cookies });
        check(res, {
            "status is 200": (r) => r.status === 200,
            "has cookie 'name'": (r) => r.json().cookies.name.length > 0,
            "has cookie 'name2'": (r) => r.json().cookies.name2.length > 0
        });

        // Since the cookies are set as "request cookies" they won't be added to VU cookie jar
        let vuJar = http.cookieJar();
        let cookiesForURL = vuJar.cookiesForURL(res.url);
        check(null, {
            "vu jar doesn't have cookie 'name'": () => cookiesForURL.name === undefined,
            "vu jar doesn't have cookie 'name2'": () => cookiesForURL.name2 === undefined
        });
    });
}
```

## Local Cookie Jar

Create a separate cookie jar for specific use cases:

<Steps>
  <Step title="Create local jar">
    ```javascript theme={null}
    import http from "k6/http";

    let jar = new http.CookieJar();
    ```
  </Step>

  <Step title="Send cookies with local jar">
    ```javascript theme={null}
    let cookies = {
        name5: "value5",
        name6: "value6"
    };
    let res = http.get("http://httpbin.org/cookies", { 
        cookies: cookies, 
        jar: jar 
    });
    ```
  </Step>

  <Step title="Check cookies in local jar">
    ```javascript theme={null}
    let cookiesForURL = jar.cookiesForURL(res.url);
    console.log(cookiesForURL);
    ```
  </Step>
</Steps>

## Advanced Cookie Management

### Setting Cookies Manually

```javascript theme={null}
import http from "k6/http";
import { check, group } from "k6";

export default function() {
    group("Advanced send with local jar", function() {
        let jar = new http.CookieJar();
        jar.set("http://httpbin.org/cookies", "name7", "value7");
        jar.set("http://httpbin.org/cookies", "name8", "value8");
        let res = http.get("http://httpbin.org/cookies", { jar: jar });
        let cookiesForURL = jar.cookiesForURL(res.url);
        check(res, {
            "status is 200": (r) => r.status === 200,
            "has cookie 'name7'": (r) => r.json().cookies.name7.length > 0,
            "has cookie 'name8'": (r) => r.json().cookies.name8.length > 0
        });

        cookiesForURL = jar.cookiesForURL(res.url);
        check(null, {
            "local jar has cookie 'name7'": () => cookiesForURL.name7.length > 0,
            "local jar has cookie 'name8'": () => cookiesForURL.name8.length > 0
        });

        // Make sure cookies have NOT been added to VU cookie jar
        let vuJar = http.cookieJar();
        cookiesForURL = vuJar.cookiesForURL(res.url);
        check(null, {
            "vu jar doesn't have cookie 'name7'": () => cookiesForURL.name7 === undefined,
            "vu jar doesn't have cookie 'name8'": () => cookiesForURL.name8 === undefined
        });
    });
}
```

### Cookie Attributes

Set cookie attributes like domain and path:

```javascript theme={null}
import http from "k6/http";
import { check, group } from "k6";

export default function() {
    group("Advanced cookie attributes", function() {
        let jar = http.cookieJar();
        jar.set("http://httpbin.org/cookies", "name9", "value9", { 
            domain: "httpbin.org", 
            path: "/cookies" 
        });

        let res = http.get("http://httpbin.org/cookies", { jar: jar });
        check(res, {
            "status is 200": (r) => r.status === 200,
            "has cookie 'name9'": (r) => r.json().cookies.name9 === "value9"
        });

        jar.set("http://httpbin.org/cookies", "name10", "value10", { 
            domain: "example.com", 
            path: "/" 
        });
        res = http.get("http://httpbin.org/cookies", { jar: jar });
        check(res, {
            "status is 200": (r) => r.status === 200,
            "doesn't have cookie 'name10'": (r) => r.json().cookies.name10 === undefined
        });
    });
}
```

## Session Persistence

Maintain user sessions across multiple requests:

```javascript theme={null}
import http from "k6/http";
import { check, group } from "k6";
import { sleep } from "k6";

export default function() {
    // Login to establish session
    let loginRes = http.post("https://example.com/login", {
        username: "user",
        password: "pass"
    });
    
    check(loginRes, {
        "login successful": (r) => r.status === 200
    });
    
    // Session cookies are automatically maintained
    sleep(1);
    
    // Make authenticated request (cookies sent automatically)
    let dashboardRes = http.get("https://example.com/dashboard");
    
    check(dashboardRes, {
        "dashboard loaded": (r) => r.status === 200
    });
    
    sleep(1);
    
    // Another authenticated request
    let profileRes = http.get("https://example.com/profile");
    
    check(profileRes, {
        "profile loaded": (r) => r.status === 200
    });
}
```

<Note>
  Each VU maintains its own cookie jar throughout the test iteration, simulating independent user sessions.
</Note>
