Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/javascript-api/k6-http/cookiejar.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/k6-http/cookiejar/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
CookieJar
CookieJar is an object for storing cookies that are set by the server, added by the client, or both. As described in the how-to guide on using Cookies, k6 handles cookies automatically by default. If you need more control over cookies you can however create your own cookie jar and select it as the active jar (instead of the default one created by k6) for one or more requests.
| Method | Description |
|---|---|
| cookiesForURL(url) | Get Object of cookies where the key is the cookie name and the value is an array. |
| set(url, name, value, [options]) | Set a cookie in the jar by specifying name, value and some other optional settings like domain, path etc. |
| clear(url) | Delete all cookies for the given URL. |
| delete(url, name) | Deletes the name cookie for the given URL. |
Example
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const res1 = http.post('https://quickpizza.grafana.com/api/cookies?my_cookie=hello%20world', {
redirects: 0,
});
const jar = http.cookieJar();
const cookies = jar.cookiesForURL('https://quickpizza.grafana.com/api/cookies');
check(res1, {
"has cookie 'my_cookie'": (r) => cookies.my_cookie.length > 0,
'cookie has correct value': (r) => cookies.my_cookie[0] === 'hello world',
});
jar.clear('https://quickpizza.grafana.com/api/cookies');
const res2 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res2, {
'has status 200': (r) => r.status === 200,
"hasn't cookie 'my_cookie'": (r) => r.json().cookies.my_cookie == null,
});
}Was this page helpful?
Related resources from Grafana Labs

