Reading each row from csv for payload

Hello Everyone, Can someone please help me with the below scenario in K6? I have a csv file with multiple rows with unique userid,password,emails that needs to be passed as parameter when doing the post request. I want to be able to pass each specific row from the csv file as a single parameter that goes inside a group and I have multiple groups defined in the default function. contents of csv file

userid,password,email
123,fghytttttrtr,abc@example.com
345,fghytttttrtr,atyc@example.com                                                                      
let data = {
  "resourceType": "Parameters",
  "parameter": [
  
        {
          "name": "userid",
          "valueString": "uiseridvalue"
        },
        {
          "name": "passwprd",
          "valueString": "passwordvalue"
        },
        {
          "name": "email",
          "valueString": "emailvalue"
        }
      ]
    }
  ]
};                                                                                                         
group('email login',function () {
       
                 
	let res1 = http.request('POST', BASE_URL, JSON.stringify(data), {

		headers: headers,

	});                                                                                                             
group('token auth',function () {


	   
	let res1 = http.request('POST', BASE_URL, JSON.stringify(data), {

		headers: headers,

});

How can I pass the first row from the csv file as data parameter for group ‘email login’ and pass the second row as data parameter for the group ‘token auth’? Appreciate the help in advance.

Hi @alex977

I think this doc about data parameterization would be useful in your case. It describes how to use paraparse lib to parse CSV and once it is parsed, you can simply address each row by index, e.g. csvData[0] and csvData[1].

Hi @olha ,
Thanks for the response. I am a newbie here so please excuse my silly queries. Is there a way to get the data from the csv file using the index in a loop? I want to be able to pass the payload with csvData[0]—[15] when doing the post.

let data = {
  "resourceType": "Parameters",
  "parameter": [
  
        {
          "name": "userid",
          "valueString": "useridvalue"  ---> each time the parameter is posted, the useridvalue value gets retrieved from the csv file uniquely? 
        },
        {
          "name": "passwprd",
          "valueString": "passwordvalue"
        },
        {
          "name": "email",
          "valueString": "emailvalue"
        }
      ]
    }
  ]
}

Thanks,
Alex

Hi @alex977,

Do you mean executing your email login/token auth requests inside a for loop? Yes, you can do that as @olha has detailed before: using the csvData[i]. Can you clarify why you need to do in this way, please? Is the k6’s iteration concept not enough for your use case?

I want also to encourage you to not wrap each request in a group, this is considered a bad practice. You can check the Discouraged use cases in the Groups documentation for finding the available alternatives.

Thanks @codebien
In fact I wanted to see if there was a better solution than wrapping each request in a group. Here is my scenario
I have the same base URL and have a csv file with unique user/password combination. I need to post a payload with everything same but different user/password from the csv during each request and assert different body text from those responses.
Is there an easy way to achieve this?
Right now each vu picks unique row from the data file and does the post in the same group only without iterating through all the groups and the test fails not able to find the display text.
Thanks,
Alex

The best way, in this case, it’s to follow the retrieving unique data guide from the same page linked in the previous response Reading each row from csv for payload - #2 by olhayevtushenko - Grafana k6 - Grafana Labs Community Forums

1 Like

am using log format as csv, end of every run my expected values got appended in that csv file
How to clean csv file before run