Skip to content

Instantly share code, notes, and snippets.

@ColinHarrington
Created July 2, 2012 19:27
Show Gist options
  • Save ColinHarrington/3035139 to your computer and use it in GitHub Desktop.
Save ColinHarrington/3035139 to your computer and use it in GitHub Desktop.
GUM JSON + HTTP Builder.
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2')
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def http = new HTTPBuilder( 'http://google.com' ) // Takes URL e.g. http://myservice:8087/
http.request( POST, JSON) { req ->
body = [ '{ "database":"tracking", "collectionData":[ { "collection":"events", "data":[ { "measurement": {"miles": 7.7, "hours": 0.9 }, "source": "onboard", "consumerId": 6, "date": {"data_type":"date_iso8601", "value":"2012-06-06T16:19:57.000Z"}, "metafields":{"name4":"value15", "name2":"value8"} } ] } ] }' ]
headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/10.10 Firefox/3.0.4'
headers.'Referer' = 'http://quicklinuxsolutions.blogspot.com/'
headers.'Content-Type' = 'application/json'
uri.path = '/data'
response.success = { resp, json ->
println "SUCCESS!!"
}
response.failure = { resp ->
println "FAIL"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment