Forum Discussion

shubhimu's avatar
shubhimu
Contributor
7 years ago

How to fetch rest get method request parameters using groovy script

Hi,

 

Please tell me the way to fetch rest get method request parameters value using groovy script.

 

Also is it possible that we can get add parameters using groovy. 

 

Kindly let me know.

Using below code i am able to get the endpoint, resource but need help in getting the parameters(query string)

 

 

import com.eviware.soapui.impl.rest.RestResource
try
{
def pro=testRunner.testCase.testSuite.project
def tcrest=testRunner.testCase.testSuite.project.getTestSuiteByName("Business Templates").getTestCaseByName("Rest_GetTemplates")
def request =tcrest.getTestStepByName("RequestTemplate_GetPromotions").httpRequest.requestContent
def tcexe=testRunner.testCase.testSuite.project.getTestSuiteByName("Execution Suite").getTestCaseByName("Execution Driver")
def endpoint=tcrest.getTestStepByName("RequestTemplate_GetPromotions").getPropertyValue("EndPoint")
String resourcepath=tcrest.getTestStepByName("RequestTemplate_GetPromotions").getResourcePath()
log.info endpoint
tcexe.getTestStepByName("REST Request Get").setPropertyValue("EndPoint", endpoint)
//String resourcepath= tcrest.getTestStepByName("RequestTemplate_GetPromotions").httpRequest.getPath()
log.info "Business template resource path is"+resourcepath
tcexe.setPropertyValue("ResourcePath", resourcepath)
}
catch(Exception e)
{
log.error " exception message is "+e.getMessage()
}

 

Thanks,

Himanshu

  • Lucian's avatar
    Lucian
    Community Hero

    Hey, this should do it:

     

    // Get the current test case
    def testCase = testRunner.testCase
    
    // Get the desired testStep
    def testStep = testCase.getTestStepByName("Request1")
    
    // Iterate through each parameter
    testStep.requestProperties.each 
    {
    // Log the existing parameters log.info it.getKey() + " : " + it.getValue().getValue() }

    // Add a new parameter called 'param3' and assign to it the value 'val3'
    testStep.getRestMethod().addProperty( "param3" ).setValue( "val3" ) 

    :catvery-happy:

    • shubhimu's avatar
      shubhimu
      Contributor

      Lucian. Thanks for this.

       

      My requirement is to take rest request from one test case lets say A and copy the endpoint/resource and parameter of the same in to the rest request placed in another test case B and run the request from B test case.

       

      Ready api has non editable endpoint/resource/parameter. And i can take the end point of rest request from its custom properties. Also i can set it in the another test case rest request but i am not able to get and set resource and parameters.

       

      You provoded the solution to get rest request parameters and add it but can you please provide me some help in getting the resource and how i can set it in another request. Also how i can set rest request parameters in another rest request.

       

      Thanks,

      Himanshu

      • Radford's avatar
        Radford
        Super Contributor

        I don't know how to do exactly what you are asking and while not the solution you need this might help you on your way, some Groovy which will get the rest resource details:

         

        log.info('REST Test Step Class = ' + testRunner.getTestCase().getTestStepByName('REST Request').getClass())
        
        log.info('Resource Name = ' + testRunner.getTestCase().getTestStepByName('REST Request').getResource().getName())
        
        def xmlBeansRestParamsTestPropertyHolder = testRunner.getTestCase().getTestStepByName('REST Request').getResource().getParams()
        
        log.info('xmlBeansRestParamsTestPropertyHolder Class = ' + xmlBeansRestParamsTestPropertyHolder.getClass())
        
        xmlBeansRestParamsTestPropertyHolder.getPropertyList().each(){ restParamsTestProperty -> 
        
        	log.info('----------------------------------------------')
        	log.info('Param Class = ' + restParamsTestProperty.getClass())
        	log.info('Param Name = ' + restParamsTestProperty.getName())  
        	log.info('Param Style = ' + restParamsTestProperty.getStyle())
        	log.info('Param Location = ' + restParamsTestProperty.getParamLocation())
        	log.info('Data Type = ' + restParamsTestProperty.getType().getLocalPart())
        
        }

         

        I assume that you've seen the Ready API JavaDocs? I think this will be a case of looking up the classes in the JavaDocs and trying things out, good luck. Please do post back here if you solve this problem.