RESTing easy with Groovy’s HTTPBuilder

how-to
Jun 1, 20093 mins

Interacting with RESTful web services couldn’t be easier with Groovy’s HTTPBuilder. This handy library has a slick extension dubbed RESTClient, which facilitates handling all aspects of REST (such as GET and POST) quite nicely; plus, parsing and building of both requests and responses in either JSON or XML is a breeze, baby!

Check it out– first, you must create an instance of RESTClient like so:

def zorillo = new RESTClient("<a href="https://acme.zorrillo.com/">https://acme.zorrillo.com/</a>")

Because it’s my bag, I’m interacting with a service whose base URI is acme.zorrillo.com. With this instance, I can easily obtain information via an HTTP GET like so:

def res = zorillo.get(path:"widget/1010081127")

In the above code, I’ve issued a GET request to <a href="https://acme.zorrillo.com/widget/1010081127">https://acme.zorrillo.com/widget/1010081127</a>, which returns an XML response that looks something like this:

<pre class="prettyprint"><code><widget id='1010081127'>
 <type>JPM</type>
</widget>

Interestingly, with an instance of a response (in my case, res), you can grab an instance of the returned XML via the data property, which is the root node returned via Groovy’s hip <a href="<a href="https://www.ibm.com/developerworks/java/library/j-pg05199/index.html">https://www.ibm.com/developerworks/java/library/j-pg05199/index.html</a>">XMLSlurper</a>. Thus, data points to the widget element; consequently, I can grab the attribute id and the corresponding element’s value quite easily:

<pre class="prettyprint"><code>assert <a href="mailto:res.data.@id.text">res.data.@id.text</a>() == '1010081127'
assert res.data.type.text() == 'JPM'

Need to post some data? That couldn’t be easier either, man — simply use RESTClient’s post method, which allows you to use a MarkupBuilder to construct corresponding XML like so:

<pre class="prettyprint"><code>def ans = zorillo.post(path:"widget/",
		requestContentType: XML,
         body: {
           widget(id:'129033'){
             type("TFR")
          }
        })

Note in this case, the POST URL is <a href="https://acme.zorrillo.com/widget/">https://acme.zorrillo.com/widget/</a> and I’m posting a document which looks like so:

<pre class="prettyprint"><code><widget id='129033'>
 <type>TFR</type>
</widget>

But, because I’m using Groovy’s MarkupBuilder I don’t have to deal with XML directly — not bad, eh? The /widget service returns an XML document of the created widget if everything works; thus, I can verify things are kosher like so:

<pre class="prettyprint"><code>assert ans.status == 200
assert <a href="mailto:ans.data.@id.text">ans.data.@id.text</a>() == '129033'
assert ans.data.type.text() == 'TFR'

How’s that for easy, man? The next time you need to interact with a RESTful web service, consider using Groovy’s HTTPBuilder — it’s a snap!

You can now follow The Disco Blog on Twitter, baby!
andrew_glover

When Andrew Glover isn't listening to “Funkytown” or “Le Freak” he enjoys speaking on the No Fluff Just Stuff Tour. He also writes articles for multiple online publications including IBM's developerWorks and O'Reilly’s ONJava and ONLamp portals. Andrew is also the co-author of Java Testing Patterns, which was published by Wiley in September 2004; Addison-Wesley’s Continuous Integration; and Manning’s Groovy in Action.

More from this author