对于涉及 HTTP 请求的 Web 客户端编程,推荐的库是什么。
我知道有一个名为HTTP的包,但它似乎不支持 HTTPS。有没有更好的图书馆呢?
我希望为 Haskell提供一个具有类似功能的库。
Network.HTTP.Conduit
有一个干净的 API(它使用Network.HTTP.Types
),如果您对管道有所了解,使用起来非常简单。例子:
{-# 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)
还要确保查阅教程。
除了Network.HTTP.Conduit
那里Network.Http.Client
公开一个io-streams
接口。
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.