haskell - 具有 FromJSON 约束的类型变量的仆人客户端

标签 haskell servant

我正在尝试使用 servant 创建到 Web API 的客户端绑定(bind)图书馆。 我希望能够发送任何 JSON 对象。

import           Control.Monad.Trans.Except (ExceptT, runExceptT)
import           Data.Proxy
import           Network.HTTP.Client (Manager)
import           Servant.API
import           Servant.Client
import           Data.Aeson

-- | This methods accepts any instance of 'ToJSON'
--   I would like to have only this method exported from the module
send :: ToJSON a => a -> Manager -> IO (Either ServantError Result)
send x manager = runExceptT $ send_ x manager baseUrl

type MyAPI a = "acceptAnyJson" 
    :> ReqBody '[JSON] a
    :> Post '[JSON] Result

api :: ToJSON a => Proxy (MyAPI a) 
api = Proxy 

send_ :: ToJSON a => a -> Manager -> BaseUrl -> ExceptT ServantError IO Result
send_ = client api

现在,当我尝试编译它时,出现错误消息:

Couldn't match type ‘a0’ with ‘a’
      because type variable ‘a’ would escape its scope
    This (rigid, skolem) type variable is bound by
      the inferred type for ‘send_’:
      ...

如何参数化我的 MyAPIclientProxy 以接受类型变量?

最佳答案

您需要将 api 的类型与您要发送的内容的类型联系起来:

{-# LANGUAGE ScopedTypeVariables #-}
send_ :: forall a. (FromJSON a) => a -> Manager -> BaseUrl -> ExceptT ServantError IO Result
send_ = client (api :: Proxy (MyAPI a))

或者为什么还要为 api 烦恼:

send_ = client (Proxy :: Proxy (MyAPI a))

关于haskell - 具有 FromJSON 约束的类型变量的仆人客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37778052/

相关文章:

haskell - MINGW64 上的堆栈测试输出乱码

haskell - 为什么 "++"功能比 ":"贵得多?

haskell - Stack.yaml 与 package.yaml 用于添加依赖项

haskell - 如何提升为Servant Server类型?

haskell webframeworks 速度,GHCi 与编译

haskell - 通过仆人在运行时更改网络根目录(或路径前缀)?

haskell - 新手: understanding main and IO()

haskell - Haskell 内置阶乘函数

haskell - 从servant-0.4.4.7迁移到servant-0.7.1

forms - 与servant一起处理常规表单帖子(application/x-www-form-urlencoded)