【问题标题】:Haskell SimpleHTTP get response codeHaskell SimpleHTTP 获取响应码
【发布时间】:2012-10-16 18:57:06
【问题描述】:

我正在尝试使用 simpleHTTP 从我认为 HTTP 类中的响应类型获取响应代码(200,404 等) 到目前为止:

--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)

【问题讨论】:

    标签: http haskell response http-response-codes


    【解决方案1】:

    我想你想要的是

    getResponseCode :: Result (Response ty) -> IO ResponseCode
    

    如果您使用的是 HTTP-4000.2.4 或更高版本,则来自 Network.HTTP 模块。对于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
    

    【讨论】:

    • kewl 会试试看,但这只会给我签名?
    • 是的,这只会给你三倍的Ints,因为Response 类型有一个rspReason 字段。
    • getResponseCode :: Result (Response ty) -> IO ResponseCode `getResponseCode' 的类型签名缺少随附的绑定
    • 您应该将其用作responseCode &lt;- getResponseCode rspgetResponseCodeNetwork.HTTP 中定义。
    • 不在范围内:getResponseCode' Perhaps you meant getResponseBody'(从 Network.HTTP 导入)
    猜你喜欢
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2011-06-25
    • 2017-10-23
    • 1970-01-01
    • 2016-02-07
    • 2017-09-26
    • 2018-06-13
    相关资源
    最近更新 更多