Clear Cookies on each iteration

How we can clear cookies on each iteration in k6 as we have similar option in other tools like jmeter and loadrunner.

I have already added the below in options, but still its not working.
noCookiesReset: false,

Hello,

Cookies are by default cleared on each iteration; noCookieReset: false is the default when not specified, so it shouldn’t be needed except for when you want to persist cookies across iterations.

Thanks @Tom for the reply, but I’m facing issue in my k6 script in which I’m getting HTTP 405 method not allowed error and the same request is working perfectly in jmeter when we have introduced clear cookies on each iteration config element.

can someone from your team can look into the issue?

Hi @divinp ,

Can you also try setting noVUConnectionReuse: true and see if that helps? (docs)

Can you also elaborate and what call fails if it still does as that seem … strange.

I have tried giving noVUConnectionReuse: true, but still the request is failing.

The call is adding an item to a cart, which is a POST method call with product quantity, product name and a token is being sent. The same requests with these parameters are passing in jmeter but fails in k6 with error 405.

Does it fail on the first iteration on the second one? Because if it’s on the first this flag will likely not help you.

Try setting params.jar to null as in

http.post(yoururl, thebody, {jar: null, <other Params such as headers here>})

Also maybe noConnectionReuse as well … although I doubt that this will get you a 405 :man_shrugging:

Its failing at the first iteration itself, I have set jar to null, but still its failing with the same error.

If it’s failing on the first iteration than I don’t think the problem is what you think it is.

Please double check your URLs and see if you can contact whoever is responsible for that API to help you out

@mstoykov the same API is passing without any change in Jmeter, so if its an issue with API then it should fail in Jmeter as well.

From the information you have given us:

  1. 405 means the method is not correct, so you are doing a GET when it should be POST or something like that. If you think you are doing the correct one, I would argue that you aren’t hitting the correct endpoint. Why? Probably networking/firewall issues. If you are hitting the correct system the status code is wrong(or confusing) - ask whoever is developing the API to help you here.
  2. Cookies should have no relevance here, especially not on the first iteration. Unless the option from jMeter is literally there to be no cookiejar, in which case setting the jar:null would’ve helped. Again this is strange - talk with whoever is developing the API
  3. jMeter and k6 are completely different software that you are likely running with completely different settings, possibly on completely different machines. All of those might have relevance which likely will be easier to understand if you talk with whoever is developing the API.

The people developing the API should have server-side logs and be able to tell you what is going wrong. All we can do in this thread is guess, at this point, especially without more information. I don’t even know what to ask you like this all seems like either you made a simple mistake or a strange problem with your API that will require someone developing it to help out from their side so :man_shrugging:

@mstoykov :
Thanks for the reply, if possible can we connect through a call which will help us to resolve the issue as quickly as possible. I can message you in slack if its okay for you.

I suspected the issue is with cookies is because when I disable HTTP Cookie manager in jmeter I started getting the same error, and when I enable the cookie manager to clear cookies on each iteration the error disappeared and it started working.

if possible can we connect through a call which will help us to resolve the issue as quickly as possible. I can message you in slack if its okay for you.

I really don’t want to go on a call and I would consider this to be outside of community support - so no. If someone else wants they are free to do it.

Again contact whoever is the developer of the API this all seems like a bug.

Thats fine, just to reiterate if it is a bug then the behavior will be same across all tools, but its happening only with k6 script, not sure what’s going wrong, anyway we are checking server logs as well to get more clarity. :slight_smile:

In case you missed it :
I suspected the issue is with cookies is because when I disable HTTP Cookie manager in jmeter I started getting the same error, and when I enable the cookie manager to clear cookies on each iteration the error disappeared and it started working.

Hey @divinp ,
Sorry I know this is an old post, but I noticed this and could not help myself to try to help.
I have had that behavior several times in JMeter.
What I would recommend is to compare the headers from the request in JMeter to the request in k6.
If it is not working in the first iteration, my feeling is that the previous step is not generating the needed cookies. Do a console output to check and compare.
Try this:

    // login and gather the needed cookies
    var res = http.post('https://test-api.k6.io/auth/cookie/login/',{"username": "Leo3","password": "123456"});

    // request with the right cookes
    var res = http.get('https://test-api.k6.io/my/crocodiles/');

    // checking if the cookies were correctly sent in the headers, also check for other items
    console.log(JSON.stringify(res.request.headers));

I know it is very late after but hope it helps.
Gracias,
Leandro

If you are doing a loop during the setup stage you must build a new CookieJar on each iteration to clear cookies:

const cookieJar = new http.CookieJar();

Then pass it to each http request:

var getResponse = http.get(urlGet, { jar: cookieJar });
var postResponse = http.post(urlPost, null, { jar: cookieJar });