Haskell library for HTTP communication
Asked Answered
C

4

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.

Creath answered 7/4, 2013 at 19:24 Comment(5)
The libcurl bindings segfault quite a lot when using SSL and multiple threads. I wouldn't recommend them to anyone under any circumstances.Revive
you could also try the new http-streams library (here's an intro) and blog about how it works for youFront
@Carl: Well then, I removed my suggestion :) To be honest, I never tried to use itDivergent
This question needs an answer that compares the different alternatives (imho).Anthill
@cic wreq is the most high level one. It makes heavy use of lens to give a consistent and a lean interface.Creath
C
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.

Creath answered 23/4, 2014 at 11:48 Comment(1)
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).Terrie
D
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.

Divergent answered 7/4, 2013 at 19:52 Comment(0)
C
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.

Creath answered 23/4, 2014 at 11:48 Comment(1)
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).Terrie
T
5

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

Thracian answered 8/4, 2013 at 2:54 Comment(0)
S
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.

Selsyn answered 3/10, 2017 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.