[Play-2.2.x scala] Routes depending on accept header

88 views
Skip to first unread message

Timo Hirt

unread,
Oct 18, 2013, 5:30:47 AM10/18/13
to play-fr...@googlegroups.com
Hi!

We have a little toy project developed with play and until now only served used HTML pages. We mostly followed REST principles, identified resources and implemented HTML pages to GET them or POST changes. But, now we would like to expose those resources as a JSON API.

So for get delivering multiple formats is a no brainer. Image a route for "GET /person", we can easily use the Accept header to identify the expected format of the response payload. 

But I wonder how to parse POST request payload like this. Let's say a user creates a Person using a HTML form. The controller takes the request and maps the request payload into objects of class Person and pass it over to a repository where it gets persisted. But how can I use the same function of the controller when the request payload is in JSON? 

I think of the following solution: A route like "POST /person" selects the controller function to call depending on the Accept header. This means there may be multiple definitions of the same URL pattern routing to different controllers. In this case we could use one uri but provide multiple implementations. The advantage of this approach is, that HTML, JSON and XML logic is not mixed in the same controller function. Is this possible in Play?

Are there any other approaches?

 

Julien Richard-Foy

unread,
Oct 18, 2013, 7:39:56 AM10/18/13
to play-fr...@googlegroups.com
Hi, the provided router only uses the HTTP verb and the URL to dispatch a request into your controller action. The remaining work (e.g. dealing with some headers) has to be made by the controller layer.

By the way, I’m not sure to understand what you want to do. Do you want to parse the request payload according to the Content-Type header (not the Accept header), or to send a different representation of your resource according to the Accept header?

The buit-in AnyContent body parser already parses the request payload according to its Content-Type and let you manipulate it as a Json value or XML, etc.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Timo Hirt

unread,
Oct 18, 2013, 8:29:10 AM10/18/13
to play-fr...@googlegroups.com

By the way, I’m not sure to understand what you want to do. Do you want to parse the request payload according to the Content-Type header (not the Accept header), or to send a different representation of your resource according to the Accept header?
I want to parse the request payload. I would like to have one URL pattern like "POST /person ...". To this URL clients can send HTML forms as well as JSON or XML documents. When the HTTP requests is routed to my controller the request payload needs to be mapped into a object of type Person. And this mapping is different for each Content-Type. Now, my question is how to handle this? Maybe it is to obvious and I don't get it.
 

The buit-in AnyContent body parser already parses the request payload according to its Content-Type and let you manipulate it as a Json value or XML, etc.
Can you point me to a source code example or project where I can see this in action?

Julien Richard-Foy

unread,
Oct 18, 2013, 8:41:12 AM10/18/13
to play-fr...@googlegroups.com
The buit-in AnyContent body parser already parses the request payload according to its Content-Type and let you manipulate it as a Json value or XML, etc.
Can you point me to a source code example or project where I can see this in action?

It gives you a request body of type AnyContent, which is documented here: http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api.mvc.AnyContent
You can then use request.body.asJson in your controller to get the JSON payload.
Reply all
Reply to author
Forward
0 new messages