Groovy HTTPBuilder - posting JSON with braces not supported?

4,918 views
Skip to first unread message

Brad Karels

unread,
Jul 2, 2012, 3:18:48 PM7/2/12
to groo...@googlegroups.com
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.

Ted Naleid

unread,
Jul 2, 2012, 3:29:57 PM7/2/12
to groo...@googlegroups.com
I think you want the body argument to be a valid groovy construct with nested maps using square brackets, not JS maps with squiggly brackets.


    body = [
      first : 'Bob',
      last : 'Builder',
      address : [
        street : '123 Some St',
        town : 'Boston',
        state : 'MA',
        zip : 12345
      ]
    ]

If it were using the style you've got below, it would have the value for address in squiggly brackets, which isn't valid groovy code.   I think if you translate what you've got into nested groovy maps, that you'll get what you want.

-Ted
--
You received this message because you are subscribed to the "Groovy Users of Minnesota" group.
 
To post to this group, send email to groo...@googlegroups.com
To unsubscribe from this group, send email to groovymn-u...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/groovymn?hl=en

Colin Harrington

unread,
Jul 2, 2012, 3:30:06 PM7/2/12
to groo...@googlegroups.com
This doesn't look like valid Groovy:

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"} } ] } ] } ]

perhaps you meant to escape that JSON and feed it in as a string?

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"} } ] } ] }' ]



Got it to compile here: https://gist.github.com/3035139

HTH

Colin Harrington
colin.ha...@gmail.com


--

Brad Karels

unread,
Jul 2, 2012, 4:11:53 PM7/2/12
to groo...@googlegroups.com
Yes - thank you.  I had valid JSON which is not valid Groovy.  Crudely, by flipping { --> [ and } --> ]  all worked as I would expect.  Even less fun/easy to read than JS, but hey...  In the end, the following body worked as follows:

 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":"value    8"] ] ] ] ] ] ] 

Thank you!
Reply all
Reply to author
Forward
0 new messages