Scotty and POST params

1.5k views Asked by At

I'm having an issue with the Scotty web server right now - rescue isn't working for unfound parameters - I'm still getting a 404 with the following code:

post "/newsletter/create" ( do
  (param "subscriber[email]") `rescue` (\msg -> text msg)
  formContent <- param "subscriber[email]"
  text $ "found! " ++ show formContent )

I can see that when I just use params instead, my data is there, and indexed with "subscriber[email]". Is there something going on with [ escaping? Any help with this would be tremendous.

1

There are 1 answers

3
jamshidh On BEST ANSWER

With some cleanup I got it to work:

{-# LANGUAGE OverloadedStrings #-}

import Web.Scotty
import qualified Data.Text.Lazy as TL

main = scotty 3000 $ do
  post "/newsletter/create" $ do
    formContent <- (param "subscriber[email]") `rescue` (\msg -> return msg)
    text $ "found! " `TL.append` formContent

I made a bunch of modifications, but the key point was that rescue is used as a wrapper around param, not to change any internal state, hence you shouldn't call it twice. The square brackets didn't cause me any trouble.