Forum Discussion

patrickschweize's avatar
New Member
10 years ago

How to access query string in ServiceV Dispatch Script?

I would like to moke the following github api request and use a ServiceV Dispatch Script to return different responsese depending on the query parameter:   /search/users?q=joe


I tried the following inside the dispatch script, but always get null as a result:


// Match based on query parameter
def queryString = mockRequest.getHttpRequest().getQueryString();


How to access query string in ServiceV Dispatch Script?

2 Replies

  • I tried out your scenario and added a VirtAction GET /path/users and two responses "Response 1" and "Response 2" with SCRIPT dispatcher having the following code: 


    if ( mockRequest.httpRequest.queryString.contains('token') ) {

        "Response 1"
        "Response 2"


    sending a GET to http://localhost:8080/path/users?q=token gives me Response 1 and anything else in the queryString returns Response 2. 


    I hope this helps a little on your way! 


    If not then can you please provide the dispatch script as a whole as well as a screenshot of the inspector content for your request inspector (the rightmost window). The more information you can provide, the better response you can get. 

  • Hi


    def queryParametersString = mockRequest.getHttpRequest().getQueryString()

    will return all of the optional parameters within the request


    you can get individual parameter values using the following

    def queryParametersVarX = mockRequest.getHttpRequest().getParameter("ADD PARAM NAME HERE")



    its worth noting that for some reason if you execute this within the ServiceV script you wont see any values, however if you have the virt running and output the values to the script log (i.e. queryParametersVarX) then send a request into the virt from the projects tab the values will appear in the log

    any logic based upon the values therefore will only be exercised at runtime


    hope that helps