http - Haskell SimpleHTTP 获取响应代码

标签 http haskell response http-response-codes

我正在尝试从 HTTP 类中使用 simpleHTTP 获取响应代码(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)

最佳答案

我觉得你想要的是

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

关于http - Haskell SimpleHTTP 获取响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12921600/

相关文章:

haskell - 仿函数列表

haskell - 为什么存在bind(>>=)?没有绑定(bind)的解决方案很丑陋的典型情况是什么?

ajax - `$.ajax` 中的 Request Timing 有时超过 1 秒,有时超过 30 毫秒

java - Android 无效使用 SingleClientConnManager : connection still allocated

HTTP 调用的 Android 随机异常。为什么?

http - RESTful 建议 : default value for given resource

session - 如何将数据放入 session 变量并在vaadin的不同页面中获取数据?

Angular : Unable to GET local json using HttpClient

haskell - 列出 Haskell 类型的构造函数名称?

javascript - 使用prototype.js获取响应头