How to pass Dynamic Infatoken In Http Get/Post request

Hello Everyone,
I have recorded a test script via k6 browser recorder. In the recorded script there are many http calls get recorded at backend where dynamic infatoken is being generated which is unique for each flow. Now i have made the script the way so that it can read the dynamic value in each execution of the test script and the code looks like this -

    const res_2 = http.get('')
    const infatoken_1 = findBetween(res_2.body, '\r\n\t\t\t\tinfaToken: ', ',\r\n\t\t\t\tlocale');
    console.log('infatoken is' + infatoken_1) 
Here it is printing the correct infatoken in logs. But when i am trying to pass it in http request, This request is getting failed.

This is how i am trying to pass the infatoken in http request - 
 response = http.get(
                headers: {
                    accept: '*/*',
                    'x-requested-with': 'XMLHttpRequest',
                    infatoken: infatoken_1,
                    'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Microsoft Edge";v="114"',
                    'sec-ch-ua-mobile': '?0',
                    'sec-ch-ua-platform': '"Windows"',
        const isCheckPassed = check(response, { 'Check response status is 200': (res) => res.status === 200, });
        if (!isCheckPassed) {
            console.log('Check failed!');
            console.log('Response body:', response.body);
                { 'Request 6': response => response.status.toString() === '200' })

Here ‘Request 6’ check is failing. And showing error- {“errorCode”:“infa.core.FORBIDDEN”,“description”:“Request forbidden. Please contact your administrator.”}

Does anyone have any idea about it. Please help to resolve this issue.

Thanks in Advance!

Hi there, welcome to the forum :slight_smile:

It’s difficult for anyone to help you with this here, since there could be many different reasons why your backend is returning that error.

Some things to think about:

  • If you’re indeed getting the right token from the first request, since you mention it’s being logged correctly, then something else might be wrong with the authentication. Confirm that you’re making the request to the correct endpoint, and that all required headers are specified correctly.

  • If you have access to the backend service, enable debug logging that would help you troubleshoot why the request failed.

  • Enable --http-debug in k6, which will show you details of the request being made.

Hope this helps!