Haskell:
Sending an HTTP request
How to:
Let’s get to the fun stuff. You’ll need http-client and http-client-tls packages. Set up your stack and add them to your package.yaml or .cabal file. Then, run stack build or appropriate commands to fetch them.
Here’s a simple GET request:
import Network.HTTP.Client
import Network.HTTP.Client.TLS
import qualified Data.ByteString.Lazy.Char8 as L8
main :: IO ()
main = do
manager <- newManager tlsManagerSettings
request <- parseRequest "http://httpbin.org/get"
response <- httpLbs request manager
L8.putStrLn $ responseBody responseThis will print the JSON you received from httpbin.org.
Deep Dive
Back in the day, Haskell’s HTTP requests were less straightforward, but libraries like http-client have simplified the process.
Alternatives? Sure. There’s wreq, req, and others, often with syntactic sugar or extra features. But http-client is like that dependable swiss army knife in your drawer – it always gets the job done.
Under the hood, http-client uses a Manager to handle connections. It’s efficient and reuses sockets. You can tune it, but defaults are fine to start.
See Also
To extend your toolkit, check these out: