packages feed

happs-tutorial-0.9.0: templates/formsanddata.st

<h3>GET, POST, and FromData</h3>
<p>We've covered how to serve basic HTML with Happstack, in a number of different ways.  What we haven't yet covered
is what to do with form data.  In principle, you could dig into the request and try to pull out the parameters.  That's
not the Happstack way, though.</p>
<p>Instead, you can use the type class FromData and the functions getData and withData.
A simple example can be found in <a href=/src/GetExample.hs>GetExample.hs<a/>.  This example uses HSP to build
the forms, for simple convenience.</p>
<p>ghci>:i FromData
<br/>class FromData a where fromData :: RqData a
<br/>        -- Defined in Happstack.Server.SimpleHTTP
<br/>instance [overlap ok] (FromData a, FromData b) => FromData (a, b)
<br/>  -- Defined in Happstack.Server.SimpleHTTP
<br/>instance [overlap ok] (FromData a, FromData b, FromData c) => FromData (a, b, c)
<br/>  -- Defined in Happstack.Server.SimpleHTTP
<br/>instance [overlap ok] (FromData a,FromData b,FromData c,FromData d) => FromData (a, b, c, d)
<br/>  -- Defined in Happstack.Server.SimpleHTTP
<br/>instance [overlap ok] (FromData a) => FromData (Maybe a)
<br/>  -- Defined in Happstack.Server.SimpleHTTP
</p>
<p>An important note about RqData is that it is a Monad and a MonadPlus, so between this type class machinery and
the helper function look, you should be able to define your own instances of FromData for your own types.
Again, the simple example up above should provide some guidance for this.</p>
<p>Next we cover <a href="/tutorial/file-uploads">uploading files</a></p>