simpleHTTP
是多態的。包含的響應主體可以是嚴格的ByteString
,惰性ByteString
或String
中的任意一種。它會選擇其中的一個匹配傳遞給它的值Request ty
。不幸的是,HTTP
是一個古老的圖書館,維護得不好 - 它最低限度地增加了與這三種類型一起工作所必需的多態性,但並非處處都可以使圖書館友好使用。例如,使用getRequest
強制響應爲String
,取消simpleHTTP
中的多態性。
下面是一個最簡單的工作示例,它抓取該URL的內容,並使用HTTP
和aeson
進行解碼。
import Network.URI
import Network.HTTP
import Data.Aeson
-- There's absolutely no reason that this function shouldn't be in HTTP.
-- It's just as unsafe and terrible as getRequest is. In fact, this is a
-- strictly more general type than getRequest has, so there's no reason
-- it shouldn't just replace it.
getRequest_ :: HStream ty => String -> Request ty
getRequest_ s = let Just u = parseURI s in defaultGETRequest_ u
main :: IO()
main = do
rsp <- simpleHTTP $ getRequest_ "http://www.reddit.com/r/haskell.json"
body <- getResponseBody rsp
print (decode $ body :: Maybe Value)
注意它的使用decode
終於引腳向下的多態型的simpleHTTP
那裏。只是在這個版本中,它並不意見getRequest
對代碼中的類型所說的話。
它確實是'HTTP'包中的'simpleHTTP'。我怎樣才能告訴它使用正確的'ByteString',這樣我就可以將它提供給'decode'函數? – BinRoot
@Nick只需將它提供給'decode'函數即可。這就是確保它是正確的類型所需要的。這是整個類型推斷的要點。 – Carl
得到'rsp'之後,我嘗試了'sb < - getResponseBody rsp'然後'decode sb',但是由於它期望'Data.ByteString.Lazy.Internal.ByteString'而導致類型錯誤失敗, Char]' – BinRoot