Created
August 3, 2012 20:10
-
-
Save axeda/3251102 to your computer and use it in GitHub Desktop.
HTTPBuilder Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovyx.net.http.* | |
import static groovyx.net.http.ContentType.* | |
import static groovyx.net.http.Method.* | |
http = new HTTPBuilder('http://ajax.googleapis.com') | |
// perform a GET request, expecting JSON response data | |
http.request( GET, JSON){ | |
uri.path = '/ajax/services/search/web' | |
uri.query = [ v: '1.0', q: 'Axeda Corp.' ] | |
headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8/10 Firefox/3.0.4' | |
// response header for a success response code: | |
response.success = { resp, json -> | |
println resp.statusLine | |
resp.headers.each { | |
println "$it.name = $it.value" | |
} | |
// parse the JSON response object: | |
json.responseData.results.each { | |
println = " ${it.titleNoFormatting} : ${it.visibleUrl}" | |
} | |
} | |
// handler for any failure status code: | |
response.failure = { resp -> | |
println "Unexpected error: ${resp.statusLine.statusCode : ${ resp.statusLine.reasonPhrase }" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment