RESTing easy with Groovy’s HTTPBuilder
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("http://acme.zorrillo.com/")
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 http://acme.zorrillo.com/widget/1010081127, which returns an XML response that looks something like this:
<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 XMLSlurper. Thus, data points to the widget element; consequently, I can grab the attribute id and the corresponding element’s value quite easily:
assert res.data.@id.text() == '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:
def ans = zorillo.post(path:"widget/",
requestContentType: XML,
body: {
widget(id:'129033'){
type("TFR")
}
})
Note in this case, the POST URL is http://acme.zorrillo.com/widget/ and I’m posting a document which looks like so:
<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:
assert ans.status == 200
assert ans.data.@id.text() == '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!
| Related odds and ends | ||
|---|---|---|
5 comments Tuesday 02 Jun 2009 | Groovy, Languages
5 Responses to “RESTing easy with Groovy’s HTTPBuilder”
Are you POSTing a or a loan()? The XML and the MarkupBuilder don’t seem to agree. But I could certainly be missing something.
Good catch, Sean — in this example, I am supposed to be working with widgets. I’ve made the correction — thanks!
wow! It looks so easy.
I am jumping in…
Maybe i am missing something, but when i tried this, I got into a nasty dependency hell. It kept throwing NoClassDefFounds for various classes like JSONObject, CatalogResolver, etc.
Any help will be appreciated
Ameya — welcome to Maven! Unfortunately, the project is distributed assuming everyone uses maven, which is far from the truth! The dependencies of HttpBuilder which I found to work for my purposes are:
Please note that these dependencies change between versions — for example, the latest version also has a dependency on xml-resolver.