I have done a bit of searching and have not found any posts or documentation around this issue, forgive me if it does overlap.
Working on a Groovy script to send JSON data to a service endpoint (this will become a service within an application in time) using HTTPBuilder. However, when my JSON contains `{` I encounter an error:
/Users/bkarels/dev/postjsontest/postOne.groovy: 13: expecting '}', found ':' @ line 13, column 23.
So it is finding error here:
body = [ { "database":
..............................^
My code is as follows (some bits removed for brevity):
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2')
import ...
def http = new HTTPBuilder( args[0] ) // 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 ->
...
}
response.failure = { resp ->
...
}
}
When I POST the same JSON through a browser based HTTP client (DEV HTTP CLIENT) at the same service all works and a row is persisted to the collection. However, when doing things this way, I get the error posted above. I have not found a case where someone is doing this with JSON more complex than nested arrays nor have I found any documentation stating that HTTPBuilder does not support JSON objects. Has any found a way to post complex JSON using Groovy either with HTTPBuilder or other? Thank you.