簡體   English   中英

使用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不是您想像的值,而是單子。 如果要獲取響應的狀態碼,可以嘗試以下操作:

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副作用的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM