[web-devel] Raw POST data

Michael Snoyman michael at snoyman.com
Tue May 31 05:11:08 CEST 2011


On Tue, May 31, 2011 at 5:23 AM, Jeremy Hughes <jedahu at gmail.com> wrote:
> Hi again,
>
> I can't find a means to access raw POST data. Both Network.Wai.Request
> and Yesod.Request.Request appear to only contain pre-parsed values.
> What am I missing?
>
> J

Hi Jeremy,

There's an example in the haskell-web-rosetta repo[1] of getting the
raw content of a request. (Next bit is slightly complicated.) The idea
is that a Handler is in fact a monad transformer on top of an
Iteratee. An Iteratee is a streaming data consumer, which allows us to
parse the request body in constant space. The "consume" function used
here turns that stream into a (strict) list of bytestrings, i.e.,
reads them all into memory. The use of lift is to deal with the fact
that Handler is a monad transformer.

So the simplest way to read the request body is the one shown in that
example. But depending on what you want to do, you could also use a
streaming approach via enumerators and thereby not read the entire
request body into memory. For example, with JSON, you could use
aeson[2] and attoparsec-enumerator[3]. But that requires a bit more
work.

What exactly do you want to do with the raw request body?

Michael

[1] https://github.com/snoyberg/haskell-web-rosetta/blob/master/json-request/yesod.hs
[2] http://hackage.haskell.org/package/aeson
[3] http://hackage.haskell.org/package/attoparsec-enumerator



More information about the web-devel mailing list