我想做一些非常简单的事情;发出wreq
响应并将内容打印到终端。
main.hs看起来像这样: { - #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)
为什么呢?因为r
似乎是一个字节串,所以它应该是无法打包的。
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
我的.cabal文件如下所示:
executable wreqscraper
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5,
wreq,
bytestring
当我尝试用stack build
构建它时,我得到了这个:
/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)
使用putStrLn
(或类似内容)打印这些内容的正确方法是什么?
答案 0 :(得分:3)
您需要打开Response ByteString
,以便获得ByteString
,然后将其与C.unpack
一起使用。
这是一个有效的例子:
{-# 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)