使用 Network.Wreq 从 Response 中提取 responseStatus
Extract responseStatus from Response using Network.Wreq
{-# 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 的新手。
正如上面的评论所指出的,在使用更复杂的库之前学习 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 副作用的方式。
{-# 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 的新手。
正如上面的评论所指出的,在使用更复杂的库之前学习 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 副作用的方式。