Haskell Scotty Webserver 发送文本响应

标签 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" }

最佳答案

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

也可以工作。

关于Haskell Scotty Webserver 发送文本响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502302/

相关文章:

haskell - 在 OS X Yosemite 10.10 (14A389) 上安装 QuickCheck for GHC 7.8.3 时出现 Clang 错误

c - 在 Xeon-Phi 上运行 Haskell

haskell - 模板 Haskell : How to extract the number of arguments of a function?

Haskell 非二叉树

haskell - 什么时候泛型函数不是泛型的?

haskell - GADT模式匹配的封装

haskell - Scotty 的 ActionT 的 MonadReader 实例

haskell - 如何使用 Scotty/wai 在代理后面记录真实 IP 地址

haskell - 关于 scotty Haskell Web 框架的简单问题

haskell - 如何与 scotty 和 Selda 一起使用 monad 堆栈?