Http 尝试使用wreq解码时出错
我真的很难理解如何使用镜头和wreq,结果它真的让我慢了下来 错误似乎表明这里有一些不匹配的类型。但我不知道该怎么处理。我对哈斯克尔还是个新手,这些镜头让人很困惑。然而,wreq看起来更干净,这就是我选择使用它的原因。有人能帮我理解错误是什么,以及如何修复它吗?我似乎遇到了很多这样的类型错误。我知道,Http 尝试使用wreq解码时出错,http,haskell,authentication,Http,Haskell,Authentication,我真的很难理解如何使用镜头和wreq,结果它真的让我慢了下来 错误似乎表明这里有一些不匹配的类型。但我不知道该怎么处理。我对哈斯克尔还是个新手,这些镜头让人很困惑。然而,wreq看起来更干净,这就是我选择使用它的原因。有人能帮我理解错误是什么,以及如何修复它吗?我似乎遇到了很多这样的类型错误。我知道,我的代码目前可能不会返回TestInfo。没关系。那个错误我知道如何处理。但是,我不知道这个错误 这是我的密码: 模块测试信息: {-# LANGUAGE OverloadedStrings #-}
我的代码目前可能不会返回TestInfo
。没关系。那个错误我知道如何处理。但是,我不知道这个错误
这是我的密码:
模块测试信息:
{-# LANGUAGE OverloadedStrings #-}
module TestInformation where
import Auth
import Network.Wreq
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens (_String)
type TestNumber = String
data TestInfo = TestInfo {
TestId :: Int,
TestName :: String,
}
instance FromJSON TestInfo
getTestInfo :: Key -> TestNumber -> Maybe TestInfo
getTestInfo key test =
decode (res ^. responseBody . _String)
where opts = defaults & auth ?~ oauth2Bearer key
res = getWith opts ("http://testsite.com/v1/tests/" ++ test)
模块认证:
module Auth where
import qualified Data.ByteString as B
type Key = B.ByteString
错误:
GHCi, version 7.10.1: http://www.haskell.org/ghc/ :? for help
[1 of 2] Compiling Auth ( Auth.hs, interpreted )
[2 of 2] Compiling TestInformation ( TestInformation.hs, interpreted )
TestInformation.hs:36:18:
Couldn't match type ‘Response body10’
with ‘IO (Response Data.ByteString.Lazy.Internal.ByteString)’
Expected type: (body10
-> Const Data.ByteString.Lazy.Internal.ByteString body10)
-> IO (Response Data.ByteString.Lazy.Internal.ByteString)
-> Const
Data.ByteString.Lazy.Internal.ByteString
(IO (Response Data.ByteString.Lazy.Internal.ByteString))
Actual type: (body10
-> Const Data.ByteString.Lazy.Internal.ByteString body10)
-> Response body10
-> Const Data.ByteString.Lazy.Internal.ByteString (Response body10)
In the first argument of ‘(.)’, namely ‘responseBody’
In the second argument of ‘(^.)’, namely ‘responseBody . _String’
TestInformation.hs:36:33:
Couldn't match type ‘Data.ByteString.Lazy.Internal.ByteString’
with ‘Data.Text.Internal.Text’
Expected type: (Data.ByteString.Lazy.Internal.ByteString
-> Const
Data.ByteString.Lazy.Internal.ByteString
Data.ByteString.Lazy.Internal.ByteString)
-> body10 -> Const Data.ByteString.Lazy.Internal.ByteString body10
Actual type: (Data.Text.Internal.Text
-> Const
Data.ByteString.Lazy.Internal.ByteString Data.Text.Internal.Text)
-> body10 -> Const Data.ByteString.Lazy.Internal.ByteString body10
In the second argument of ‘(.)’, namely ‘_String’
In the second argument of ‘(^.)’, namely ‘responseBody . _String’
Failed, modules loaded: Auth.
Leaving GHCi.
此类型为我检查:
getTestInfo :: Key -> TestNumber -> IO (Maybe TestInfo)
getTestInfo key test = do
res <- getWith opts ("http://testsite.com/v1/tests/" ++ test)
return $ decode (res ^. responseBody)
where opts = defaults & auth ?~ oauth2Bearer key
getTestInfo::Key->TestNumber->IO(可能是TestInfo)
getTestInfo键测试=do
请注意,第一条错误消息与wreq
或lens
无关,与不理解IO
有关。第二条错误消息与wreq
或lens
无关,与将字节流与文本数据混淆有关。decode(…)
是一个可能是TestInfo
,但getTestInfo
是一个IO操作<代码>返回
将纯值转换为IO操作(在本例中)。getTestInfo
的调用者将使用一元绑定操作符从IO操作中获取返回值-就像它与IO操作getWith
一起使用以获取其返回值一样。它实际上似乎不起作用。抱歉,lpaste版本不正确。使用答案中给出的getTestInfo
。以下是更新的lpaste: