It looks like you're integrating with some kind of RESTful web service. There is a REST client plugin associated here .
Alternatively, this is fairly easy to do without the plugin associated here .
I highly recommend that your controller be just a controller. Present your interface with this external service in some class, for example, OtherApiService or some kind of utility. Keep all the code that communicates with this external service in one place; this way you can mock the integration component and easily test everywhere. If you do this as a service, you have the opportunity to expand, say, if you want to start storing some data from the API in your own application.
In any case, cutting and posting from the related documentation (second link) shows how to send GET to the API and how to configure handlers for success and failures, as well as process request headers and request parameters - this should be all you need.
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' ) import groovyx.net.http.* import static groovyx.net.http.ContentType.* import static groovyx.net.http.Method.* def http = new HTTPBuilder( 'http://ajax.googleapis.com' ) // perform a GET request, expecting JSON response data http.request( GET, JSON ) { uri.path = '/ajax/services/search/web' uri.query = [ v:'1.0', q: 'Calvin and Hobbes' ] headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4' // response handler for a success response code: response.success = { resp, json -> println resp.statusLine // parse the JSON response object: json.responseData.results.each { println " ${it.titleNoFormatting} : ${it.visibleUrl}" } } // handler for any failure status code: response.failure = { resp -> println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}" } }
You can also check this out for some nifty tricks. There is an example with the POST method.
source share