Haskell Spock: How to get the raw request body

657 Views Asked by At

The body function in Web.Spock.Action is supposed to return the raw request body. However, it just doesn't seem to be doing that:

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Data.Text.Encoding (decodeUtf8)
import Debug.Trace        (trace)
import Web.Spock
import Web.Spock.Config

app :: SpockM () () () ()
app = do
    get root $ text "Hello!"
    post "test" $ do
        b <- body       -- b is always ""!
        text $ trace ("b="++show b) decodeUtf8 b

main :: IO ()
main = do
       spockCfg <- defaultSpockCfg () PCNoDatabase ()
       runSpock 3000 (spock spockCfg app)

A curl --data 123123 localhost:3000/test returns nothing, and the trace output confirms that b is an empty string.

Spock is running on port 3000
b=""

The equivalent Scotty app works just fine:

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Data.Text.Lazy.Encoding (decodeUtf8)
import Web.Scotty

main = scotty 3000 $ do
    get "/" $ text "Hello!"
    post "/test" $  do
        b <- body       -- works fine
        text $ decodeUtf8 b

I absolutely can't see what I'm doing wrong. Any input would be highly appreciated!

Update: Above example will work with Spock >= 0.12.0.0!

0

There are 0 best solutions below