使用Network.Wreq从Response中提取responseStatus

时间:2017-01-04 14:53:01

标签: haskell ghci

{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
import Data.ByteString.Lazy
import Control.Lens

totalResponse :: IO (Response ByteString)
totalResponse =  response

status :: Status
status =  response ^. responseStatus

response = get "url"

给出了

getRequest.hs:10:23: error:
    • Couldn't match type ‘Response body0’
                     with ‘IO (Response ByteString)’
      Expected type: Getting Status (IO (Response ByteString)) Status
        Actual type: (Status -> Const Status Status)
                     -> Response body0 -> Const Status (Response body0)
    • In the second argument of ‘(^.)’, namely ‘responseStatus’
      In the expression: response ^. responseStatus
      In an equation for ‘status’: status = response ^. responseStatus

当我寻找

:type response ^. responseStatus

ghci中给出了

response ^. responseStatus :: Status

我对Haskell来说是全新的。

1 个答案:

答案 0 :(得分:1)

正如上面的评论指出的那样,在使用更复杂的库之前学习Haskell可能是个好主意。如上所述,您的代码中存在类型不匹配。 response不是你想象的那个值,而是一个monad。如果您想获得回复的状态代码,可以试试这个:

status :: IO Status
status = do
  resp <- get "url" -- This is how the value returned by get is obtained.
  return (resp ^. responseStatus)

请注意,一切都在IO monad中完成,这是Haskell处理IO副作用的方式。