I want to do something very simple; make a wreq
response and print the contents to the terminal.
main.hs looks like this: {-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Wreq
import qualified Data.ByteString.Char8 as C
main :: IO ()
main = do
r <- get "http://httpbin.org/get"
putStrLn (C.unpack r)
Why? Because r
seems to be a bytestring, so it should be unpackable.
Prelude> import Network.Wreq
Prelude Network.Wreq> r <- get "http://httpbin.org/get"
Prelude Network.Wreq> :type r
r :: Response Data.ByteString.Lazy.Internal.ByteString
My .cabal file looks like this:
executable wreqscraper
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5,
wreq,
bytestring
And when I try to build it with stack build
I get this:
/Users/mkaravan/end2end/haskell_sandbox/wreqscraper/src/Main.hs:22:22: error:
• Couldn't match expected type ‘C.ByteString’
with actual type ‘Response
Data.ByteString.Lazy.Internal.ByteString’
• In the first argument of ‘C.unpack’, namely ‘r’
In the first argument of ‘putStrLn’, namely ‘(C.unpack r)’
In a stmt of a 'do' block: putStrLn (C.unpack r)
What is the correct way to print these contents with putStrLn
(or something similar)?
You need to unwrap the Response ByteString
, so you get a ByteString
and you can use it with C.unpack
.
Here is a working example:
{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
import Control.Lens
import Data.ByteString.Lazy.Char8 as C
main :: IO ()
main = do
r <- get "http://httpbin.org/get"
Prelude.putStrLn $ C.unpack $ (r ^. responseBody)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.