22

对于涉及 HTTP 请求的 Web 客户端编程,推荐的库是什么。

我知道有一个名为HTTP的包,但它似乎不支持 HTTPS。有没有更好的图书馆呢?

我希望为 Haskell提供一个具有类似功能的库。

4

4 回答 4

14

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)       

还要确保查阅教程

于 2013-04-07T19:52:42.573 回答
12

Bryan O'Sullivan 已经发布了一个名为wreq的库,它非常好用并且易于用于 HTTP 通信。

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

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

于 2014-04-23T11:48:32.543 回答
5

除了Network.HTTP.Conduit那里Network.Http.Client公开一个io-streams接口。

于 2013-04-08T02:54:30.380 回答
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.

于 2017-10-03T01:21:36.247 回答