httphaskelltimeouthttp-conduit

Increasing request timeout for Network.HTTP.Conduit


I use the http-conduit library version 2.0+ to fetch the contents from a HTTP webservice:

import Network.HTTP.Conduit
main = do content <- simpleHttp "http://stackoverflow.com"
          print $ content

As stated in the docs, the default timeout is 5 seconds.

Note: This question was answered by me immediately and therefore intentionally does not show further research effort.


Solution

  • Similar to this previous question you can't do that with simpleHttp alone. You need to use a Manager together with httpLbs in order to be able to set the timeout.

    Note that you don't need to set the timeout in the manager but you can set it for each request individually.

    Here is a full example that behaves like your function above, but allows you to modify the timeout:

    import Network.HTTP.Conduit
    import Control.Monad (liftM)
    import qualified Data.ByteString.Lazy.Char8 as LB
    
    -- | A simpleHttp alternative that allows to specify the timeout
    -- | Note that the timeout parameter is in microseconds!
    downloadHttpTimeout :: Manager -> String -> Int -> IO LB.ByteString
    downloadHttpTimeout manager url timeout = do req <- parseUrl url
                                                 let req' = req {responseTimeout = Just timeout}
                                                 liftM responseBody $ httpLbs req' manager
    
    main = do manager <- newManager conduitManagerSettings
              let timeout = 15000000 -- Microseconds --> 15 secs
              content <- downloadHttpTimeout manager "http://stackoverflow.com" timeout
              print $ content