-
Book Overview & Buying
-
Table Of Contents
Mastering play framework for scala
By :
In Play, the response to a request is a result. A result has two components: the response header and the response body. Let's look at a simple example of this:
def plainResult = Action {
Result(
header = ResponseHeader(200, Map(CONTENT_TYPE -> "text/plain")),
body = Enumerator("This is the response from plainResult method".getBytes())
)
}Notice that we used an enumerator for the response body. An enumerator is a means to provide data to an iteratee. We will discuss these in detail in Chapter 6, Reactive Data Streams.
Apart from this, a result has additional functions that equips us with better means to handle response headers, sessions, cookies, and so on.
A result can send JSON, XML, and images as a response, apart from a String content. An easier way of generating a result is to use the result helpers. A result helper is used for most of the HTTP response status. As an example, let's see how the TODO Action that comes built in with Play is implemented...
Change the font size
Change margin width
Change background colour