Tom has posted 1 posts at DZone. View Full User Profile

HTTPBuilder 0.3.1 released

  • submit to reddit

HTTPBuilder is a Groovy wrapper around Apache's HttpClient, with a response handling model similar to Prototype.js' Ajax.Request.  It also supports automatic response parsing and request building based on content-type, and convenience features for common authentication and content-encoding schemes.

 HTTPBuilder can be used for simple ad-hoc HTTP queries, or as a REST client like so:

// Use Google's JSON API to perform a search query:
builder.request( '', GET, JSON ) {
url.path = '/ajax/services/search/web'
url.query = [ v:'1.0', q: 'Calvin and Hobbes' ]
headers.'User-Agent' = "Mozilla/5.0"

// the response is automatically parsed into a JSON object:
response.success = { resp, json ->
assert json.size() == 3

println "Query response: "
json.responseData.results.each {
// print each search result and the URL:
println " ${it.titleNoFormatting} : ${it.visibleUrl}"

 See more examples here.

Built-in content-types (supported for parsing and POSTing data) include:

  • XML and HTML (using XmlSlurper and NekoHTML)
  • JSON (thanks to json-lib)
  • form-urlencoded (transformed to/from a Map)
  • text (as a Reader and Writer, automatically using the correct charset)
  • binary (using InputStream/ OutputStream)

Latest binaries and source packages are available on Maven and the site's download page.
Published at DZone with permission of its author, Tom Nichols.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)