Skip to content
REST client plugin that uses Spring's RestTemplate
Branch: master
Clone or download
Latest commit 17f3037 Oct 6, 2016
Type Name Latest commit message Commit time
Failed to load latest commit information.
src converted to Grails 3 Sep 3, 2015
.classpath cleanup Dec 4, 2012
.gitignore converted to Grails 3 Sep 3, 2015
.project cleanup Dec 4, 2012
.travis.yml add travis tokens Jun 24, 2014
LICENSE added license file Sep 3, 2015 remove version Oct 6, 2016
build.gradle converted to Grails 3 Sep 3, 2015 converted to Grails 3 Sep 3, 2015
settings.gradle converted to Grails 3 Sep 3, 2015 add travis ci config Jun 24, 2014

Build Status

Rest Client Builder Grails Plugin


The code for this plugin has moved to a subproject of the Grails Data. Please submit any pull requests there.


Edit BuildConfig.groovy and add the following dependency:

compile ":rest-client-builder:2.0.0"

For Grails 3.x this plugin is no longer necessary and you should instead just declare a dependency on the core Grails Data library:

 compile 'org.grails:grails-datastore-rest-client'

Basic Usage

For API-style documentation refer to the Groovydocs

The main entry point is the class. Construct and use one of the REST "verbs".

A GET request:

 def resp = rest.get("")

The response is a Spring ResponseEntity.

There are convenience methods for obtaining JSON:

  resp.json instanceof JSONObject == 'acegi'

And XML:

  resp.xml instanceof GPathResult == 'acegi'

POST and PUT requests

POST and PUT requests can be issued with the post and put methods respectively:

        def resp = rest.put(""){
            auth System.getProperty("artifactory.user"), System.getProperty("artifactory.pass")
            contentType "application/"
            json {
                name = "test-group"
                description = "A temporary test group"

In the example above the auth method performs HTTP basic auth, the contentType method sets the content type, and the json method constructs a JSON body.

A POST with URL parameters

        def resp ='http://someServer/foo/bar?username={username}&password={password}') {
            urlVariables [username:"someDude", password:"abc#123"]

Please note the above url is not a GString with String interpolation but a String that uses the format/convention specified in the RestTemplate javadoc.

Exchanging JSON and XML content

As demonstrated in the previous example you can send JSON data using the json method which accepts the same syntax as Grails' JSONBuilder. Alternatively you can provide a String, or any other valid Java object that is convertable to JSON. The following are also valid examples:

        def resp = rest.put(""){
            contentType "application/"
            json name: "test-group", description: "A temporary test group"

        def resp = rest.put(""){
            contentType "application/"
            json '{ name: "test-group", description: "A temporary test group" }'

If you don't explicitly set the contentType like is done above then the default content type for the json method is "application/json".

XML is very similar, there is an xml method that takes a closure to build XML, and object which is convertable to XML or a String of XML.

The response object has xml or json properties for reading the response of the request as demonstratd in the "Basic Usage" section.

Sending custom headers

You can include in your request any additional header:

def resp = rest.put("") {
    header 'X-Auth-Token', '1a2b3c4d5e6f7g8h'


Testing can be done with Spring's RestTemplate mocking APIs. See the tests for RestBuilder itself for an example.

Multipart Requests

Multipart requests are possible by setting properties of the request body to File, URL, byte[] or InputStream instances:

    def resp = {
        contentType "multipart/form-data"
        zip = new File(pluginPackage)
        pom = new File(pomFile)
        xml = new File(pluginXmlFile)

Connection/Proxy Configuration

Connection and proxy configuration can be specified in the constructor to RestBuilder:

def rest = new RestBuilder(connectTimeout:1000, readTimeout:20000, proxy:['localhost':8888])

The proxy setting can either be map containing the key for the host name and a value for the port or an instance of

You can’t perform that action at this time.