Servant QueryParams parse error


Servant QueryParams parse error



Given the following code:


newtype HelloMessage = HelloMessage { msg :: String }
deriving (Generic)

instance ToJSON HelloMessage

type API2 = "hello"
:> QueryParam "age" Int
:> Get '[JSON] HelloMessage

appAPI2 :: Proxy API2
appAPI2 = Proxy

myHandler :: Server API2
myHandler = helloHandler
where
helloHandler :: Maybe Int -> Handler HelloMessage
helloHandler mAge =
let
sAge = case mAge of
Nothing -> "0"
Just ag -> show ag
in
return . HelloMessage $ sAge

app2 :: Application
app2 = serve appAPI2 myHandler

main :: IO ()
main = run 8080 app2



I can access /hello which returns { "msg" : 0} or /hello?age=20 which returns { "msg" : 20}. But if I set the age to be a non-integer parseable e.g. "foobar" which makes the url to be /hello?age=foobar and access it, it returns an error message about parsing error on "foobar".


/hello


{ "msg" : 0}


/hello?age=20


{ "msg" : 20}


"foobar"


/hello?age=foobar


"foobar"



This behave differently with Capture where if I give the same treatment it'll just return a http 400.


Capture



What is wrong my code?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift