2012-10-16 25 views
2

我想從我與simpleHTTP 的HTTP類思考如何回答類型迄今爲止獲得響應代碼(200404等):哈斯克爾SimpleHTTP得到響應代碼

--how to get the http response Int from response 
getStatusCode response = print 0 

--this works... 
--- othercode --- 
rsp <- simpleHTTP (defaultGETRequest_ clean_uri) 
file_buffer <- getResponseBody(rsp) 
--this fails 
response = (getStatusCode rsp) 

回答

3

我想你想要的是

getResponseCode :: Result (Response ty) -> IO ResponseCode 
Network.HTTP模塊

如果你使用HTTP-4000.2.4或更高版本。對於早期版本的HTTP,顯然您必須在rspCode字段上進行模式匹配,類似於以下rspReason字段所示的方式。

如果你有興趣的原因,使用ResponserspReason場,後

rsp <- simpleHTTP ... 

你有

rsp :: Either ConnError (Response ty) -- Result is a type synonym for (Either ConnError) 

,並且可以訪問每

let reason = case rsp of 
       Left err -> show err -- for example 
       Right response -> rspReason response 
putStrLn $ "Here's why: " ++ reason 
+0

KEWL意志的原因試試看,但那隻能給我簽名? – user914584

+0

是的,這隻給你一個'Int'的三倍,因爲'Response'類型有一個'rspReason'字段。 –

+0

getResponseCode :: Result(Response ty) - > IO ResponseCode getResponseCode' 的類型簽名缺少相應的綁定 – user914584