Forum Discussion

avidCoder's avatar
avidCoder
Super Contributor
7 years ago

How to make the REST request JSON Key's Value dynamic?

Let say, I have following request as JSON:-   { "code":"${DataSource#code}", "ciDate":"${Properties#StartDate}", "coDate":"${Properties#EndDate}" }   I want to make ciDate and coDate dynamic ...
  • JHunt's avatar
    7 years ago

    The Open Source way to do it would be to run a Groovy Script in the same Test Case as your Test Request. As an example:

     

    def step = testRunner.testCase.getTestStepByName("My REST Test Step")
    def data = [
        [startDate: "01/01/2001", endDate: "02/02/2002"],
        [startDate: "03/03/2003", endDate: "04/04/2004"],
        [startDate: "05/05/2005", endDate: "06/06/2006"]
    ]

    data.each {
        testRunner.testCase.setPropertyValue("startDate", it.startDate)
        testRunner.testCase.setPropertyValue("endDate", it.endDate)
        testRunner.runTestStep( step )
        log.info step.testRequest.responseContentAsString // just logging it here but you could write to file etc
    }

    And request body as:

    {
    	"code":"${DataSource#code}",
    	"ciDate":"${#TestCase#startDate}",
    	"coDate":"${#TestCase#endDate}"
    }

    There may be easier ways to do it in Pro, but I wouldn't know about that. I mention it because you have mentioned a DataSource, which I believe is only for Pro.