Suppose we have a Haskell data type and value:
We could represent that data as HTML:
<table> <tr> <th>Name</th> <td>Michael</td> </tr> <tr> <th>Age</th> <td>25</td> </tr> </table>
or we could represent it as JSON:
or as XML:
<person> <name>Michael</name> <age>25</age> </person>
Oftentimes, web applications will use a different URL to get each of these
/person/michael.json, etc. Yesod follows the
RESTful principle of a single URL for each resource. So in Yesod,
all of these would be accessed from
Then the question becomes how do we determine which representation to
serve. The answer is the HTTP
Accept header: it gives a prioritized
list of content types the client is expecting. Yesod will automatically determine which
representation to serve based upon this header.
Let’s make that last sentence a bit more concrete with some code:
chooseRep function takes two arguments: the value
we are getting representations for and a list of content types that the client will
accept. We determine this by reading the
chooseRep returns a tuple containing
the content type of our response and the actual content.
This typeclass is the core of Yesod’s RESTful approach to representations. Every ...