garbagetown/playdocja — pull request #633 by wm3 (2014-03-22 08:41:06+00:00)
Title: manual/scalaGuide/main/xml/ScalaXmlRequests.md

- https://github.com/garbagetown/playdocja/blob/2.2.0/documentation/2.2.0/manual/scalaGuide/main/xml/ScalaXmlRequests.md
```
--- /Users/garbagetown/Desktop/2.1.5/manual/scalaGuide/main/xml/ScalaXmlRequests.md	2013-09-19 22:53:02.000000000 +0900
+++ /Users/garbagetown/Desktop/2.2.0/manual/scalaGuide/main/xml/ScalaXmlRequests.md	2013-09-19 10:11:22.000000000 +0900
@@ -2,35 +2,15 @@

## Handling an XML request

-An XML request is an HTTP request using a valid XML payload as the request body. It must specify the `text/xml` MIME type in its `Content-Type` header.
+An XML request is an HTTP request using a valid XML payload as the request body. It must specify the `application/xml` or `text/xml` MIME type in its `Content-Type` header.

By default an `Action` uses a **any content** body parser, which lets you retrieve the body as XML (actually as a `NodeSeq`):

-'''scala
-def sayHello = Action { request =>
- request.body.asXml.map { xml =>
- (xml \\ "name" headOption).map(_.text).map { name =>
- Ok("Hello " + name)
- }.getOrElse {
- BadRequest("Missing parameter [name]")
- }
- }.getOrElse {
- BadRequest("Expecting Xml data")
- }
-}
-'''
+@[xml-request-body-asXml](code/ScalaXmlRequests.scala)

It’s way better (and simpler) to specify our own `BodyParser` to ask Play to parse the content body directly as XML:

-'''scala
-def sayHello = Action(parse.xml) { request =>
- (request.body \\ "name" headOption).map(_.text).map { name =>
- Ok("Hello " + name)
- }.getOrElse {
- BadRequest("Missing parameter [name]")
- }
-}
-'''
+@[xml-request-body-parser](code/ScalaXmlRequests.scala)

> **Note:** When using an XML body parser, the `request.body` value is directly a valid `NodeSeq`.

@@ -38,7 +18,7 @@

'''
curl
- --header "Content-type: text/xml"
+ --header "Content-type: application/xml"
--request POST
--data 'Guillaume'
http://localhost:9000/sayHello
@@ -58,24 +38,16 @@

In our previous example we handle an XML request, but we reply with a `text/plain` response. Let’s change that to send back a valid XML HTTP response:

-'''scala
-def sayHello = Action(parse.xml) { request =>
- (request.body \\ "name" headOption).map(_.text).map { name =>
- Ok(Hello {name})
- }.getOrElse {
- BadRequest(Missing parameter [name])
- }
-}
-'''
+@[xml-request-body-parser-xml-response](code/ScalaXmlRequests.scala)

Now it replies with:

'''
HTTP/1.1 200 OK
-Content-Type: text/xml; charset=utf-8
+Content-Type: application/xml; charset=utf-8
Content-Length: 46

Hello Guillaume
'''

-> **Next:** [[Handling file upload | ScalaFileUpload]]
\ No newline at end of file
+> **Next:** [[Handling file upload | ScalaFileUpload]]

```