Haskell Scotty Webserver发送文本回复

时间:2015-02-13 14:56:34

标签: haskell scotty

我收到GET请求并希望发送短信作为对它的回复。我有以下代码但是收到以下错误

{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Network.Wai.Middleware.RequestLogger

import Data.Monoid (mconcat) 

main = scotty 4000 $ do
middleware logStdoutDev

get "/" $ do
    beam<- param "q"
    text $ "The message which I want to send"

我尝试运行服务器时遇到的错误是

No instance for (Parsable t0) arising from a use of ‘param’
The type variable ‘t0’ is ambiguous
Note: there are several potential instances:
  instance Parsable () -- Defined in ‘scotty-0.9.1:Web.Scotty.Action’
  instance Parsable Bool
    -- Defined in ‘scotty-0.9.1:Web.Scotty.Action’
  instance Parsable Data.ByteString.Lazy.Internal.ByteString
    -- Defined in ‘scotty-0.9.1:Web.Scotty.Action’
  ...plus 9 others
In a stmt of a 'do' block: beam <- param "q"
In the second argument of ‘($)’, namely
  ‘do { beam <- param "q";
        text $ "The message I want to send" }’
In a stmt of a 'do' block:
  get "/"
  $ do { beam <- param "q";
         text $ "The message I want to send" }

1 个答案:

答案 0 :(得分:7)

param的类型为Parsable a => Text -> ActionM a。如您所见,a是多态的,只需要Parsable a的实例。它也是返回类型。但是你可以看到in the documentation Parsable有几个实例可用! GHC不知道你想要什么,因此模棱两可的错误。您需要指定一个类型注释,让GHC知道您想要什么。但无论如何,让我们编写代码

beam <- param "q" :: ActionM Text

应该做的伎俩。现在beam应该是Text。但也许你期望你的参数是一个整数,所以像

beam <- param "q" :: ActionM Integer

也可以。