I'm trying to get the response code (200,404 etc) from i THINK a Response type in the HTTP class with simpleHTTP Thus far:

--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)
有帮助吗?

解决方案

I think what you want is

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

from the Network.HTTP module if you're using HTTP-4000.2.4 or later. For earlier versions of HTTP, you would have to pattern-match yourself on the rspCode field apparently, similar to the way shown below for the rspReason field.

If you are interested in the reason, use the rspReason field of Response, after

rsp <- simpleHTTP ...

you have

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

and can access the reason per

let reason = case rsp of
               Left err -> show err  -- for example
               Right response -> rspReason response
putStrLn $ "Here's why: " ++ reason
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top