You are httpSource
looking for httpSource
from the latest version of http-conduit
. This behaves exactly like Python requests: you return a stream of pieces.
save to file
It's easy, just redirect the source directly to the sink of the file.
#!/usr/bin/env stack {- stack --install-ghc --resolver nightly-2016-11-26 runghc --package http-conduit -} {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Simple (httpSource, getResponseBody) import Conduit main = runConduitRes $ httpSource "http://httpbin.org/get" getResponseBody .| sinkFile "data_file"
print only a (take 5) byte response
Once we have the source, we take the first 5 bytes with takeCE 5
, and then print them through printC
.
#!/usr/bin/env stack {- stack --install-ghc --resolver nightly-2016-11-26 runghc --package http-conduit -} {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Simple (httpSource, getResponseBody) import Data.ByteString (unpack) import Conduit main = runConduitRes $ httpSource "http://httpbin.org/get" getResponseBody .| takeCE 5 .| printC
save to file and print only a (take 5) byte response
To do this, you want zipSinks
or for more general cases involving ZipSink
several ZipSink
drains:
#!/usr/bin/env stack {- stack --install-ghc --resolver nightly-2016-11-26 runghc --package http-conduit -} {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Simple (httpSource, getResponseBody) import Data.ByteString (unpack) import Data.Conduit.Internal (zipSinks) import Conduit main = runConduitRes $ httpSource "http://httpbin.org/get" getResponseBody .| zipSinks (takeCE 5 .| printC) (sinkFile "data_file")
source share