Created
December 11, 2013 16:14
-
-
Save azusa/7913341 to your computer and use it in GitHub Desktop.
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
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' ) | |
import groovyx.net.http.* | |
import static groovyx.net.http.ContentType.* | |
import static groovyx.net.http.Method.* | |
def 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: 'Calvin and Hobbes' ] | |
headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4' | |
// response handler for a success response code: | |
response.success = { resp, json -> | |
println resp.statusLine | |
// 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