Example usage, posting a JSON object?

26 views
Skip to first unread message

Scott Morrison

unread,
Jan 2, 2012, 4:02:39 PM1/2/12
to Bowler Users
Hi all,

could someone point me to an example of posting a JSON object to
Bowler?

I'd like to be able to write (in my controller; X and Y are both case
classes that serialize to JSON nicely).

def `POST /compute`(x: X): Y = {
x.process
}

and then in javascript something like:

$.ajax({
type: "POST",
url: "/compute",
dataType: "json",
data: { "x" : x },
success: function(y){
alert(JSON.stringify(y));
}
});
}

where x contains the JSON representation of an X.

Code as above doesn't work for me, however, giving errors like:

[debug] Response already committed for handling
net.liftweb.json.JsonParser$ParseException: unknown token o
[debug] Near: [object Object]ource%5D%5Bobjects

best regards,
Scott Morrison

Wille Faler

unread,
Jan 3, 2012, 5:22:32 AM1/3/12
to bowler...@googlegroups.com
Hi,
A couple of things that might help you:
Firstly, with the default Bowler setup, you'll need to add the correct headers to receive and post JSON:
"Content-Type: application/json" to post JSON (tell Bowler that it should look for JSON content in the body of the request)
"Accept: application/json" (to tell Bowler you want JSON back instead of markup)

In JQuery, I think you add them in the "headers" option to your Ajax request, but you might want to double-check the exact syntax.

The second thing is the shape of the JSON request itself: The standard shape of an object is an unnamed object with attributes, so in the case of posting { "x" : x }, Bowler would expect a case class that has an attribute of x in the controller route signature, rather than x directly.

Finally, we use lift-json under the hood, so all JSON objects must be based on case classes due to lift-json restrictions, unless you write your own JSON serializer and replace the default one.

Hope this helps, let us know if you have any further problems! 

Scott Morrison

unread,
Jan 3, 2012, 10:22:02 AM1/3/12
to bowler...@googlegroups.com
Thanks Wille (and also more generally for the great Bowler framework)

I think the thing that was tripping me up was the shape of the JSON
request itself; I was passing { "x": x, "y": y } and hoping to pass
that to a route with two parameters. In the meantime, I worked out the
issue by using JSON.stringify on my JSON objects in the browser, then
using StringTransformers to parse the JSON back out at the server end.
Obviously that work is being done under the hood anyway, and isn't
overly cumbersome for me (I only have a few types of JSON objects, at
least for now).

best regards,
Scott Morrison

Reply all
Reply to author
Forward
0 new messages