从 cURL IO 响应中获取 CurlCode - Haskell
Get CurlCode form cURL IO response - Haskell
我对 Haskell 比较陌生,在解析 cURL 请求的响应时遇到了一些问题。
这是我目前的代码:
import Control.Monad
import Network.Curl
import Data.Aeson
getReq :: URLString -> [CurlOption] -> IO (CurlCode, String)
getReq url opt = curlGetString url opt
当我使用getReq函数时,
Prelude> getReq "http://google.com" []
我收到这样的回复:
(CurlOk, "<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">...)
我想做的只是以某种方式解析 CurlCode (CurlOk)。它是一种 IO 类型,所以我对如何从中获取 CurlCode 感到困惑。
非常感谢任何帮助!
这里有一些关于您可以做什么的想法。它们中的任何一个接近你想做的事情吗?
getCurlCode :: IO (CurlCode, String) -> IO CurlCode
getCurlCode res = do
(cc, _) <- res
return cc
printCurlCode :: IO (CurlCode, String) -> IO ()
printCurlCode res = do
(cc, _) <- res
print cc
printStatus :: IO (CurlCode, String) -> IO ()
printStatus res = do
cc <- getCurlCode res
if cc == CurlOk
then putStrLn "Everything okay"
else putStrLn "Maybe not okay"
curlMessage :: CurlCode -> String
curlMessage cc =
case cc of
CurlOk -> "Everything okay"
_ -> "Maybe not okay"
printCurlMessage :: IO (CurlCode, String) -> IO ()
cc <- getCurlCode res
let msg = curlMessage cc
putStrLn msg
你可以这样使用getReq
:
do
(c, _) <- getReq ...
因为 getReq
returns 一个 IO
monad 而不是一个正常值,它必须在另一个 IO
monad 中使用。
您可以从 Monad
了解更多关于 monad 和 do-notation 的信息
我对 Haskell 比较陌生,在解析 cURL 请求的响应时遇到了一些问题。
这是我目前的代码:
import Control.Monad
import Network.Curl
import Data.Aeson
getReq :: URLString -> [CurlOption] -> IO (CurlCode, String)
getReq url opt = curlGetString url opt
当我使用getReq函数时,
Prelude> getReq "http://google.com" []
我收到这样的回复:
(CurlOk, "<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">...)
我想做的只是以某种方式解析 CurlCode (CurlOk)。它是一种 IO 类型,所以我对如何从中获取 CurlCode 感到困惑。
非常感谢任何帮助!
这里有一些关于您可以做什么的想法。它们中的任何一个接近你想做的事情吗?
getCurlCode :: IO (CurlCode, String) -> IO CurlCode
getCurlCode res = do
(cc, _) <- res
return cc
printCurlCode :: IO (CurlCode, String) -> IO ()
printCurlCode res = do
(cc, _) <- res
print cc
printStatus :: IO (CurlCode, String) -> IO ()
printStatus res = do
cc <- getCurlCode res
if cc == CurlOk
then putStrLn "Everything okay"
else putStrLn "Maybe not okay"
curlMessage :: CurlCode -> String
curlMessage cc =
case cc of
CurlOk -> "Everything okay"
_ -> "Maybe not okay"
printCurlMessage :: IO (CurlCode, String) -> IO ()
cc <- getCurlCode res
let msg = curlMessage cc
putStrLn msg
你可以这样使用getReq
:
do
(c, _) <- getReq ...
因为 getReq
returns 一个 IO
monad 而不是一个正常值,它必须在另一个 IO
monad 中使用。
您可以从 Monad
了解更多关于 monad 和 do-notation 的信息