dithegrey
10 years agoOccasional Contributor
REST Mocking with Query Parametr
I have several mocks with responses. But for some of them I want to create response based on additional query parameter. For example: I have mock for REST request such as "GET /order/item" and it wor...
- 8 years ago
Open the request action.
Change the Dispatch dropdown to SCRIPT
You should then be able to script something like this: (actual working example from one of my mock services)
// Match based on query parameter def queryString = mockRequest.getRequest().getQueryString() log.info "QueryString: " + queryString def response if (queryString !=null) { String[] fields = queryString.split('/&') fields.each { // Split each query string field into its key and value String[] kvp = it.split('='); def key = kvp[0]; def value = kvp[1] ; // Change the response depending on the value of nrd:AreaCode if( key == "nrd:AreaCode" ) { if( value == "7A1" ) { // return list of BCU GP Practices response = "Response - GP Practices - BCU" } else if( value == "7A2" ) { // return list of Hywel Dda GP Practices response = "Response - GP Practices - Hywel Dda" } } } } if (response != null) { log.info( "response: " + response) return response }
Apologies in advance regarding coding conventions / style. I am not a Groovy expert but I thought a working example would be helpful