3

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!

mcmayer
  • 1,931
  • 12
  • 22
  • Hm. I can reproduce but am not certain yet how the problem arises. I see when using [request](https://hackage.haskell.org/package/Spock-0.10.0.1/docs/Web-Spock-Shared.html#g:3) instead of `body`, that the `requestBodyLength` and `Content-Length` fields are set to the correct length, but the content doesn't show up so far. – Jakob Runge Jan 20 '17 at 09:57
  • 2
    Can you try this using master? I've done some changes and added test cases: https://github.com/agrafix/Spock/blob/46a86e1a109ff74f44168d3373fdb9ed0dd87f71/Spock-core/test/Web/Spock/FrameworkSpecHelper.hs#L79-L82 – agrafix Jan 20 '17 at 11:23
  • Yes that works! Thanks a lot! – mcmayer Jan 21 '17 at 06:15

0 Answers0