Forum Discussion

Sirisha2020's avatar
Sirisha2020
Contributor
15 days ago

I need help on code snippet to pass the value from groovy script to REST request payload

Hi All,

Can anyone please share a code snippet on how to pass the value from groovy script to a variable(i.e. id) that is present in REST request.

E.g. For( i=1; i=i++; i<=10)

i want to pass the above i value to REST request payload "id"field.

Thanks.

  • Humashankar's avatar
    Humashankar
    Champion Level 3

    Hi Sirisha2020 

    Give a try with the below Groovy,

    // Loop through the range of values

    for (int i = 1; i <= 10; i++) {

        // Set the 'id' property for each iteration

        testRunner.testCase.setPropertyValue("id", i.toString())

       

        // Optionally, trigger the REST request step (if you want it to execute on each loop)

        // Assuming the REST request step name is 'YourRESTRequestStep'

        def testStep = testRunner.testCase.getTestStepByName("YourRESTRequestStep")

        if (testStep != null) {

            testRunner.runTestStepByName("YourRESTRequestStep")

        } else {

            log.error "Test step 'YourRESTRequestStep' not found."

        }

    }

    Hope this helps - Happy to help further!!
    Thank you very much and have a great one!
    Warm regards