22

What is the recommended library for web client programming which involves HTTP requests.

I know there is a package called HTTP but it doesn't seem to support HTTPS. Is there any better library for it ?

I expect a library with functionality something like this for Haskell.

Sibi
  • 47,472
  • 16
  • 95
  • 163
  • 5
    The libcurl bindings segfault quite a lot when using SSL and multiple threads. I wouldn't recommend them to anyone under any circumstances. – Carl Apr 07 '13 at 21:15
  • 2
    you could also try the new [http-streams](http://hackage.haskell.org/packages/archive/http-streams/0.4.0.1/doc/html/Network-Http-Client.html) library (here's an [intro](http://blogs.operationaldynamics.com/andrew/software/haskell/http-streams-introduction)) and blog about how it works for you – jberryman Apr 08 '13 at 01:38
  • @Carl: Well then, I removed my suggestion :) To be honest, I never tried to use it – Niklas B. Apr 08 '13 at 14:22
  • 1
    This question needs an answer that compares the different alternatives (imho). – cic Mar 10 '15 at 14:40
  • 1
    @cic `wreq` is the most high level one. It makes heavy use of lens to give a consistent and a lean interface. – Sibi Mar 10 '15 at 18:06

4 Answers4

14

Network.HTTP.Conduit has a clean API (it uses Network.HTTP.Types) and is quite simple to use if you know a bit about conduits. Example:

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Data.Conduit
import Network.HTTP.Conduit
import qualified Data.Aeson as J

main =
  do manager <- newManager def
     initReq <- parseUrl "https://api.github.com/user"
     let req = applyBasicAuth "niklasb" "password" initReq
     resp <- runResourceT $ httpLbs req manager

     print (responseStatus resp)
     print (lookup "content-type" (responseHeaders resp))

     -- you will probably want a proper FromJSON instance here,
     -- rather than decoding to Data.Aeson.Object
     print (J.decode (responseBody resp) :: Maybe J.Object)       

Also make sure to consult the tutorial.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
12

A library named wreq has been released by Bryan O'Sullivan which is great and easy to use for HTTP communication.

A related tutorial for that by the same author is here.

There is also another library named req which provides a nice API.

Sibi
  • 47,472
  • 16
  • 95
  • 163
  • 4
    It uses `lens` and expects you to use it as well. Just FYI for readers because this personally bugs me (since `lens` is not idiomatic whatsoever). – MasterMastic Jul 07 '16 at 21:57
5

In addition to Network.HTTP.Conduit there Network.Http.Client which exposes an io-streams interface.

J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
1

Servant is easy to use (albeit hard to understand) and magical. It lets you specify the API as an uninhabited type, and generates request and response behaviors based on it. You'll never have to worry about serialization or deserialization, or even JSON -- it converts JSON to and from native Haskell objects automatically, based on the API. It's got an excellent tutorial, too.

Jeffrey Benjamin Brown
  • 3,427
  • 2
  • 28
  • 40