[web-devel] Raw POST data
jedahu at gmail.com
Tue May 31 08:42:17 CEST 2011
On Tue, May 31, 2011 at 3:11 PM, Michael Snoyman <michael at snoyman.com> wrote:
> Hi Jeremy,
> There's an example in the haskell-web-rosetta repo 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.
I'm reasonably ok with transformers. The requests in my app run inside
a ReaderT ErrorT stack that uses phantom types to enforce some
security invariants at compile time.
> 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 and attoparsec-enumerator. But that requires a bit more
> What exactly do you want to do with the raw request body?
Somewhat similar to my previous question to this list: I need to write
a POST/PUT proxy that passes the request body on without parsing it.
Since some of the requests may attach image or video files, a constant
memory solution is needed.
Clearly I need to knuckle down and actually read the Enumerator
documentation. It's a credit to the Yesod API that I haven't needed to
until now :-).
>  https://github.com/snoyberg/haskell-web-rosetta/blob/master/json-request/yesod.hs
>  http://hackage.haskell.org/package/aeson
>  http://hackage.haskell.org/package/attoparsec-enumerator
More information about the web-devel