You can use the plugin to host Grails , which allows you to execute a REST request in json and allows you to handle all the different types of responses. It is very easy to use.
One example might be something like:
import groovyx.net.http.HTTPBuilder import groovyx.net.http.ResponseParseException import net.sf.json.JSONException import static groovyx.net.http.ContentType.JSON import static groovyx.net.http.Method.GET ... def restMethod() { try { def http = new HTTPBuilder("http://www.example.com") def path = "/exampleService/info" http.request(Method.POST, JSON) {req -> uri.path = path contentType = 'application/json; charset=UTF-8' body = "{\"arg1\":\"value4Arg1\"}" response.success = {resp, json -> // do something with the json response } response.failure = {resp -> // return some error code } } } catch (JSONException jsonException) { // do something with the exception } catch (ResponseParseException parseException) { // do something with the exception } catch (Exception e) { // do something with the exception } }
source share