4

I am trying to use Wai and Warp to write a modest HTTP server and I am stuck trying to read POST/PUT request's bodies to extract form parameters. When I do the following

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai.Handler.Warp (run)
import qualified Data.ByteString.Char8 as C
import Network.Wai.Parse (parseRequestBody, lbsSink)
import Network.Wai(Response(..))
import Network.HTTP.Types(status200)
import Blaze.ByteString.Builder

main = run 3000 app

app req = do
  (params, _) <- parseRequestBody lbsSink req
  let r = C.concat $ map (\(x,y) -> C.concat [x,y]) params
  return $ ResponseBuilder 
      status200
      [("Content-Type", "text/plain")]
      $ fromByteString r

and then I try a simple request like

curl -o - -X POST http://localhost:3000/ -d  name=toto    

it appears my parameters don't get passed to other server, or rather do not get decoded properly as nothing is returned.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
insitu
  • 4,488
  • 3
  • 25
  • 42
  • I just tried your code on the latest versions of `warp`, `wai` and `wai-extra`, and it prints `nametoto` as expected. Note that it does _not_ print a newline, so perhaps your shell is overwriting it with the prompt? Try piping the result to a file to check if this is the case. – hammar Nov 17 '11 at 23:53
  • 1
    OK. Definitely need more sleep and forbid myself posting questions after 10pm ! Thanks for pointing me at this. – insitu Nov 18 '11 at 06:34

0 Answers0