Forum Discussion

mvnarendra's avatar
mvnarendra
Occasional Contributor
7 years ago

How to add or set query params in rest test step using groovy script

Hi,

 

I am trying to create a template test step to run different Rest requests by updating path, endpoint, resource, headers and query params using the Groovy script. I am able to set all required fields except query params. Can someone please help me how to set/add query params to Rest Test Step using groovy?

  • mvnarendra's avatar
    mvnarendra
    Occasional Contributor

    Answering my own question.

     

    I have used below code to set query params to REST request.

     

    String targetQueryString = "";
    if (mockRequest.getHttpRequest().getQueryString() != null) {
    targetQueryString = mockRequest.getHttpRequest().getQueryString();
    }
    Project project = new WsdlProject();
    RestServiceConfig serviceConfig = RestServiceConfig.Factory.newInstance();
    RestService service = new RestService((WsdlProject) project, serviceConfig);
    RestResourceConfig resourceConfig = RestResourceConfig.Factory.newInstance();
    RestResource resource = new RestResource(service, resourceConfig);
    RestMethodConfig methodConfig = RestMethodConfig.Factory.newInstance();
    RestMethod restMethod = new RestMethod(resource, methodConfig);
    RestRequestConfig requestConfig = RestRequestConfig.Factory.newInstance();
    RestRequest restRequest = new RestRequest(restMethod, requestConfig, false);
    if (targetQueryString.length() > 0) {
    for (String param : targetQueryString.split("&")) {
    restMethod.setPropertyValue(param.split("=")[0], param.split("=")[1]);
    }
    }