Handling Cookies from Setup function to Default function

Hi Team,

I am writing a test for an api, which requires cookies based authentication from the previous login api e.g.

first execute login api, get cookies and then pass these cookies in next apis.

My classes looks like-

where request method contains the method to executes different request, e.g. login.request.js have the code to execute login request and return the cookies.

Now what I am trying is, in my test class, creating a setup method, where I am calling this login api function and then getting the cookies from login.request.js, which is working fine…In setup function I am able to get cookies, Now I am writing my test like -

export default function (data) {

but in default function cookies values are not coming up.

referring to - Document how to transfer cookies from setup() to all VUs · Issue #199 · grafana/k6-docs · GitHub

Hi @vish_s02

Thanks for sharing your question.

I will need a bit more context to help.

  • Will all the VUs run the same cookie? If not, you could probably have each VU get their cookie in the VU code.
    • Or are you using the setup to login all the different VUs and keep the array of cookies to use on each Vu?
  • Can you share the (sanitized) setup() code? We can have a look at why the data.cookies is not coming up.

Thanks in advance for providing more context.


Thanks @eyeveebee for reply.

I cross check this again and its working fine, my code looks below like -

const loginData = JSON.parse(open(Utils.getResourcePath() + "users.json"));
export function setup() {
    let loginPageRequest = new LoginPageRequest();
    let position = Math.floor(Math.random() * loginData.users.length);
    let credentials = loginData.users[position];
    var response = loginPageRequest.login(credentials, successfulLogins, checkFailureRate);
    new Map(Object.entries(response.cookies)).forEach((v, k) => {
    return response.cookies

export default function(data) {
   var cookies = Object.keys(data).map(x => {
    return x + '=' + data[x][0].Value;

Hi @vish_s02

Thanks for getting back.

I understand the code works for you now? Let me know if otherwise.

For most purposes, k6 transparently manages the reception, storage, and transmission of cookies manages as described in the cookies documentation. However, you can interact with cookies during the test if needed. You might be able to simplify by using the CookieJar object in the http/k6 module. Additional examples in Cookies Example.

I hope this helps.