Extract responseStatus from Response using Network.Wreq

167 views Asked by At
{-# 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"

which gives

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

when I look for

:type response ^. responseStatus

in ghci it gives

response ^. responseStatus :: Status

I'm completely new to Haskell.

1

There are 1 answers

0
Damian Nadales On BEST ANSWER

As the comments above pointed out, it is probably a good idea to learn Haskell before using more complex libraries. As stated, you have a type mismatch in your code. response is not a value as you might think, but a monad. If you want to get the status code of your response you could try this:

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

Note that everything is done inside the IO monad, which is the way Haskell has to deal with IO side-effects.