garbagetown/playdocja — pull request #611 by wm3 (2014-03-22 08:41:00+00:00)
Title: manual/scalaGuide/main/http/ScalaContentNegotiation.md

- https://github.com/garbagetown/playdocja/blob/2.2.0/documentation/2.2.0/manual/scalaGuide/main/http/ScalaContentNegotiation.md
```
--- /Users/garbagetown/Desktop/2.1.5/manual/scalaGuide/main/http/ScalaContentNegotiation.md	2013-09-19 22:53:02.000000000 +0900
+++ /Users/garbagetown/Desktop/2.2.0/manual/scalaGuide/main/http/ScalaContentNegotiation.md	2013-09-19 10:11:22.000000000 +0900
@@ -1,6 +1,6 @@
# Content negotiation

-Content negotiation is a mechanism that makes it possible to serve different representation of a same resource (URI). It is useful *e.g.* for writing Web Services supporting several output formats (XML, JSON, etc.). Server-driven negotiation is essentially performed using the `Accept*` requests headers. You can find more information on content negotiation in the [[HTTP specification|http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html]].
+Content negotiation is a mechanism that makes it possible to serve different representation of a same resource (URI). It is useful *e.g.* for writing Web Services supporting several output formats (XML, JSON, etc.). Server-driven negotiation is essentially performed using the `Accept*` requests headers. You can find more information on content negotiation in the [HTTP specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html).

# Language

@@ -12,15 +12,7 @@

Actually, the `Accept` header does not really contain MIME types but media ranges (*e.g.* a request accepting all text results may set the `text/*` range, and the `*/*` range means that all result types are acceptable). Controllers provide a higher-level `render` method to help you to handle media ranges. Consider for example the following action definition:

-'''
-val list = Action { implicit request =>
- val items = Item.findAll
- render {
- case Accepts.Html() => Ok(views.html.list(items))
- case Accepts.Json() => Ok(Json.toJson(items))
- }
-}
-'''
+@[negotiate_accept_type](code/ScalaContentNegotiation.scala)

`Accepts.Html()` and `Accepts.Json()` are extractors testing if a given media range matches `text/html` and `application/json`, respectively. The `render` method takes a partial function from `play.api.http.MediaRange` to `play.api.mvc.Result` and tries to apply it to each media range found in the request `Accept` header, in order of preference. If none of the acceptable media ranges is supported by your function, the `NotAcceptable` result is returned.

@@ -30,12 +22,7 @@

See the API documentation of the `play.api.mvc.AcceptExtractors.Accepts` object for the list of the MIME types supported by Play out of the box in the `render` method. You can easily create your own extractor for a given MIME type using the `play.api.mvc.Accepting` case class, for example the following code creates an extractor checking that a media range matches the `audio/mp3` MIME type:

-'''
-val AcceptsMp3 = Accepting("audio/mp3")
-...
-render {
- case AcceptsMp3() => ...
-}
-'''
+@[extract_custom_accept_type](code/ScalaContentNegotiation.scala)

-> **Next:** [[Asynchronous HTTP programming | ScalaAsync]]
\ No newline at end of file
+
+> **Next:** [[Asynchronous HTTP programming | ScalaAsync]]

```