Do you have an example of returning XML or JSON for a GET or a POST?

Asked by snoopy3368

The couple of sample apps I've found are pretty good, if you are doing a normal web app. I'm interested in doing an app where the media type that is returned is JSON or XML. Do you hae such an example? Is it just so simple that its obvious? I can assume that GET would be pretty easy, but I'm not sure how to do a POST.

Question information

Language:
English Edit question
Status:
Solved
For:
play framework Edit question
Assignee:
No assignee Edit question
Solved by:
Jean-Francois POUX
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
Best Jean-Francois POUX (jfp-taldius) said :
#1

Hi,

It should be quite straightforward. Instead of using the standard render method at the end of your action, in your controller, you can call the renderJSON, renderText or renderXML methods. It should generate the correct replies, wether you're doing get or post.

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#2

Thanks Jean-Francois POUX, that solved my question.

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#3

So the renderJSON worked just find.

I couldn't find a renderXML, at least not on Controller. Perhaps I don't have the correct version? Or its located on some other Play! object?

Also, I couldn't figure out the POST. Do you have an example? I can really adapt with either documentation or an example.

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#4

I figured out how to do JSON in a post. I was introducing the error, by not specifying the content-type correctly. Once I switched that then I was successful in posting.

I'd still like to understand where renderXML is located, as I can't find that in r197 or r205.

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#5

content type should be set to 'application/json' in the post.

Also, you can only post one json object. I was trying to do multiple json objects, but the JSON parsing in Play! can't match the array of json objects up with a method signature. I've decided that for me I won't use batch operations, for now. Each post will be able to post one object. I.e. waiting for XML support as I think then the post/method args can be matched pretty easily.

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#6

I found the renderXml in r205, not sure how I missed it. So it should be pretty straight forward. I'll have to look at the post for XML now. I'm going to keep my question opened until I can solve the whole get/post for XML too.

BTW this is causing me to rethink how to structure the app for these different rendering concerns. I'll open a different question for that shortly.

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#7

quoted from another question:

There's a sample app showing how you can force a particular render or let Play! decide which template (json/xml/etc) to use. See http://www.playframework.org/manual/contents/templateselection

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#8

Thanks Jean-Francois POUX, that solved my question.

Revision history for this message
xiaocase (xiaocase1528) said :
#9

hi, snoopy3368
I can't download the examples form http://www.playframework.org/manual/contents/templateselection, Is there any problem?