Ask a Question

Groovy and File datasource


Groovy and File datasource


I'm using Ready Api 2.6 to test REST web services

I'm trying to access to a File Datasource (CSV) with groovy script

I have created a File datasource in my testcase, the list of items seems ok, good columns name and data

Im creating a groovy script to generate a POST json to include in a REST web service step


I'm looking for sample groovy script to access to the datasource infos

- number of data lines

- loop through rows

- get columns values for a specific row


I've found some expand example but i didnt succeed to user them correctly

Do you have some example that i can use and adapt


Thank you

Best regards








New Contributor

Community Hero

Hey @Ludovic,

Whats the reason you need groovy? Where you say [quote: Im creating a groovy script to generate a POST json to include in a REST web service step] i dont understand why youre not using the OTB functionality to do this.

Later on in your post you seem to state you want the groovy to process and reports certain datasource stats.

Also you mentioned ypuve found some groovy you thought might work if updated...f you have some groovy, even if it doesnt work its alwaysbest to add it to your post so the groovy scripters on the forum (not me!) will have a better understanding of where youre at in regards to progress.

Example csv and request payload can only help too (although i always forget this bit too!)


if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

Hello rich

Thanks for your answer


Sorry I don't know what OTB means... you have a link or tutorial ?


I have to generate a small json object to use as POST in my REST web service

I'm use to do it in groovy and store a variable in context


for the first time I'm using datasource to read a set of external parameters,for example





I have created a datasource FILE step named "DataSource" and the csv seems correctly loaded

I'm looping in datasource with datasource loop, seems ok


In my groovy script I need to get the 3 fields for each line, manipulate the data and build a json structure


def jsonAsText = '''{
"field1" : "string",
"field2" : "string",
"field3" : "string",
def object = slurper.parseText(jsonAsText)


I found some groovy examples to get datasource fields like


def row = testRunner.testCase.testSteps["DataSource"].currentRow;   

def thetitle = context.expand( '${DataSource#TITLE}' )


I looking for a full example of FILE datasource usage in groovy


Thanks for your help










SmartBear Alumni (Retired)

Hi @Ludo15101966,


Was the solution found, or do you need any more assistance? 

Tanya Yatskovskaya
SmartBear Community and Education Manager

Frequent Contributor

@Ludo15101966 , if your json payload is simple and small you do not need any groovy script. Instead insert json payload as below in your rest step. 

"field1" : ${DataSource#Title},
"field2" : ${DataSource#MinDate},
"field3" : ${DataSource#MaxDate},


In order to facilitate above json you should configure your datadource as shown in the picture. (I also overlaid csv file for your reference)



Hope this helps.


Showing results for 
Search instead for 
Did you mean: