Skip to content

Instantly share code, notes, and snippets.

@andrevdm
Last active August 2, 2021 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrevdm/b44473470e124ffa065ccc7ef586c71d to your computer and use it in GitHub Desktop.
Save andrevdm/b44473470e124ffa065ccc7ef586c71d to your computer and use it in GitHub Desktop.
Haskell chunked/streamed file downloads with progress notifications
import Data.Conduit ((.|))
import qualified Data.Conduit as C
import qualified Data.Conduit.Combinators as C
import Network.HTTP.Req ((/:))
import qualified Network.HTTP.Req as R
import qualified Network.HTTP.Req.Conduit as R
downloadChunkedUrl
:: FilePath
-> (ByteString -> IO ())
-> IO ()
downloadChunkedUrl dest onProgress = do
let httpsUrl = undefined -- Set however you usually do with req
let httpsOptions = mempty
R.runReq R.defaultHttpConfig $ do
R.reqBr R.GET httpsUrl R.NoReqBody httpsOptions $ \r ->
C.runConduitRes $
R.responseBodySource r
.| C.iterM (liftIO . onProgress)
.| C.sinkFile dest
pure ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment