cancel
Showing results for 
Search instead for 
Did you mean: 

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

Highlighted
Occasional Contributor

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?

1 REPLY 1
Highlighted
Occasional Contributor

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

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]);
}
}
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors